As part of a method to check the existence of specific free functions, I have a type that is designed to be implicitly convertible to any other type:
struct AnyConvert
{
template <class Dest>
operator Dest & ();
template <class Dest>
operator Dest const & () const;
};
I use AnyConvert
as a catchall in function parameters I'm not picky about when checking if the function exists. Note that this type is only ever used for metaprogramming at compile time.
This works well except for the following scenario:
template <class T> struct SomeType {};
template <class T>
int someFunction( SomeType<T> );
int someOtherFunction( SomeType<int> );
decltype( someOtherFunction( AnyConvert() ); // works
decltype( someFunction( AnyConvert() ); // does not work
I don't have any advance knowledge to the type AnyConvert is supposed to convert to. I also cannot modify the type that it should implicitly convert to. I tried adding functions like:
template <template <typename> class Dest, class ... T>
operator Dest<T...> & ();
to AnyConvert
, but this doesn't seem to solve the problem either. The compiler error generated tends to look something like:
‘AnyConvert’ is not derived from ‘SomeType<T>’
So my question is: How can I create a type that will implicitly convert to templated types when the template type is not specified? It might turn out that this isn't possible, in which case I'll need to come up with a better way to check that a subset of function parameters are correct.