8

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?

Community
  • 1
  • 1
lurscher
  • 25,930
  • 29
  • 122
  • 185
  • 1
    I like this question, but I'm interested to know what the application(s) of such a facility might be, if it is even possible. – Component 10 Jan 09 '13 at 18:37
  • 1
    @Component10, look at the linked question; this would detect when such a cast is safe or not. The cast is safe as long as no specialization exists for a const qualification of the parameter type – lurscher Jan 09 '13 at 18:39
  • Understood - I can see how this could be useful. – Component 10 Jan 09 '13 at 18:45
  • 1
    @lurscher Actually it wouldn't be safe to do so, because the class could contain an instance of another template class which is specialised for `const` types, no? – Seth Carnegie Jan 09 '13 at 18:47
  • 1
    I suspect the problem is you are doing relatively simple reasoning about a Turing complete language without taking into account its Turing completeness. `Belong_To_Same_Templ_Definition` doesn't deduce much of anything useful, given how powerful templates are in C++. – Yakk - Adam Nevraumont Jan 09 '13 at 19:13

1 Answers1

3

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.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • so, i'm still trying to figure out a way to make sure my cast is safe... it seems that is not possible using compile-time assertions then – lurscher Jan 09 '13 at 19:49
  • @lurscher I think it's impossible to make sure the cast is safe for an arbitrary template. You'll have to make a new `vector` and move/copy the elements into it. – Seth Carnegie Jan 09 '13 at 19:57
  • i know, you are probably right, but it feels so wasteful, if only the std:vector would support this with some std::vector& as_vector_of_consts() – lurscher Jan 09 '13 at 20:06
  • 1
    I agree, but I think the solution is to mix `vector` with `vector const &` (and appropriate const iterators etc.) instead of `vector` and `vector` – Useless Jan 10 '13 at 09:31