Search code examples
c++c++11templatestemplate-templatesusing-declaration

Template Template Parameters: What rule is applied in the following example


Assume the following example

using namespace std;
template <template <typename> class>
struct X 
{
   X() 
   { 
      std::cout << "1"; 
   }
};

template <typename>
struct Y {};

template <typename T>
using Z = Y<T>;

 template <>
 struct X<Y> 
 {
   X() 
   { 
      std::cout << "2"; 
    }
 };

 int main() 
 {
   X<Y> x1;
   X<Z> x2;
 }

The expression X<Y> x1 it is clear that is use the specialization that prints "2"

The second one is strange. Doing analysis the X<Z> is translated to X< Y < T > >. I expect to print "1" . But running the code this prints "2". Which rule is applied in the second one?


Solution

  • The second one is strange. Doing analysis the X<Z> is translated to X< Y < T > >. I expect to print "1" . But running the code this prints "2".

    No.

    You have that Z<T> is defined as Y<T> so Y and Z are the same thing.

    And isn't true that X<Z> is translated to X<Y<T>> (and X<Y<T>> can't match because Y<T> is a type where X accept only template-template arguments).