views:

71

answers:

2

Any chance to use enable_if with a type conversion operator? Seems tricky, since both return type and parameters list are implicit.

+1  A: 

dixit the documentation:
There does not seem to be a way to specify an enabler for a conversion operator. Converting constructors, however, can have enablers as extra default arguments.

Ugo
A: 

While I can understand the theoritecal interest in the question, I personally refrain from using conversion operators as much as possible.

The only one I ever use with consistence is the conversion to a pseudo-boolean (using the Safe Bool idiom), for smart-pointers or proxies, and as noted I use a trick to actually prevent the full boolean semantic...

If I ever want to facilitate conversions, I much prefer something along the line of:

template <class T>
T to() const;

which does not suffer from the limitations (in term of signature) of the conversion operator and requires explicit invocation, just because it's a bit clearer.

Matthieu M.