Any chance to use enable_if
with a type conversion operator? Seems tricky, since both return type and parameters list are implicit.
views:
71answers:
2dixit 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.
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.