Question

I am creating a custom type. Ideally it would be as interchangeable with a basic type as possible. To this end I have overloaded all the operators on the class, and provided a templated constructor to accept all the basic types. Evaluation of mixed types always promotes to my custom type.

auto result = 5 / mycustomtype * 2.0f; // result is of type MyCustomType

Now I've provided implementations for the int, float, and double operators. Ideally the typecasts should work as users might expect them to for any type.

It's the addition of typecast operators which has put me in an odd spot:

With code:

if ( mycustomtype > 5 ) { ... }

The compiler sees many possibilities including this example:

if ( mycustomtype > MyCustomType( 5 ) ) { ... }

or

if ( (int)mycustomtype > 5 ) { ... }

As such it gives me an error for every possible combination of types it could convert to. I understand why it has all these options but I am stuck as to how I can resolve this. The simple way out is to not support the type conversions in that manner and rather provide an interface like:

auto f = mycustomtype.As< float >()

However it would be really neat if I could have my cake and eat it too.

PS: example error output --

c:\...\MyType.cpp(106): error C2666: 'MyType< template params >::operator >=' : 4 overloads have similar conversions
      with
      [
          template specializations...
      ]
      c:\...\MyType.h(63): could be 'bool MyType<...>::operator >=(const MyType<...> &) const'
      with
      [
          template specializations...
      ]
      or       'built-in C++ operator>=(float, double)'
      or       'built-in C++ operator>=(int, double)'
      or       'built-in C++ operator>=(double, double)'
      while trying to match the argument list '(MyType, double)'
Was it helpful?

Solution

With C++11 you could mark the type conversion operators operator int() {...} as explicit (see here).

So you either have to use C++11 and explicit conversion, or forget about these type conversion operators.

By the way, in C++11 there are also user-defined literals:

MyType operator"" _mytype (unsigned long long n) {
    return MyType{n};
}

With this you can write:

if(mycustomtype > 5_mytype) { ... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top