Inspired by this question, i'm wondering if there is some compile-time check one can introduce to detect if two given template instantiations:
template <typename T>
class Templ...
typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;
are constructed from the same definition, or if they are built from different specializations of the Templ
template
so basically the hypothetical template function will behave like this:
template <typename T>
class Templ
{}
template <>
class Templ<std::string>
{}
template <>
class Templ<double>
{}
template <typename T1,typename T2>
class Belong_To_Same_Templ_Definition
{}
//tests
typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;
typedef Templ<int> intInstance;
typedef Templ<char> charInstance;
assert( Belong_To_Same_Templ_Definition< intInstance , charInstance >::value == true);
assert( Belong_To_Same_Templ_Definition< intInstance , doubleInstance >::value == false);
assert( Belong_To_Same_Templ_Definition< stringInstance , doubleInstance >::value == false);
is possible to create this kind of metafunction?
It seems unlikely, to be honest (although I can't definitively rule out a cunning trick).
There is no first-class identity for a given specialization (outside the type arguments that select it), to compare.
So, you could make it work with your own templates, if you want, but you can't write an ad-hoc inference for existing templates.
Consider also that it wouldn't work anyway, in the sense that it couldn't tell whether two instantiations have a compatible layout: even if Templ<int>
and Templ<char>
are instantiated from the same template code, with no specialization, that code can use traits classes which are specialied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With