C++ return type overload hack

后端 未结 7 649
无人及你
无人及你 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 10:03

    In your example, you allow casts to int and float. As long as these two casts perform the same basic logic, i.e. operator int() { return operator float(); } I see no problem. If they behave differently, this could definitely result in some surprises or ambiguities. This is because I would expect numeric results to have a coherent meaning.

    0 讨论(0)
提交回复
热议问题