C++ return type overload hack

后端 未结 7 677
无人及你
无人及你 2021-02-04 09:02

I was bored and came up with such hack (pseudocode):

 1 struct proxy {
 2     operator int(); // int function
 3     operator double(); // double function
 4             


        
7条回答
  •  故里飘歌
    2021-02-04 09:54

    If you really mean something like this:

     1 struct proxy {
     2     operator long() { return refs.first; } // long has greater precision
     3     operator double() { return refs.second; } // double has greater range
     4     proxy( long const &, double const & );
     5     pair< long const &, double const & > refs;
     6 };
     7
     8 proxy function() {
     9     return proxy( numeric_limits::max() + 1,
                         double( numeric_limits::max() ) );
    10 }
    11 int v = function(...);
    12 double u = function(...);
    

    Then yes, I think that's cool and I would count it as a hack.

    If it works. I didn't test it at all.

提交回复
热议问题