Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板参数:以下示例中应用了什么规则_C++_C++11_Templates_Template Templates_Using Declaration - Fatal编程技术网

C++ 模板参数:以下示例中应用了什么规则

C++ 模板参数:以下示例中应用了什么规则,c++,c++11,templates,template-templates,using-declaration,C++,C++11,Templates,Template Templates,Using Declaration,假设下面的例子 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 <&

假设下面的例子

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;
 }
使用名称空间std;
模板
结构X
{
X()
{ 
std::cout
。我希望打印“1”。但运行此代码将打印“2”。第二个规则中应用了哪条规则

第二个很奇怪。分析
X
会转换为
X
。我希望打印“1”。但运行此代码会打印“2”

没有

你知道
Z
被定义为
Y
,所以
Y
Z
是一样的


如果将
X
转换为
X
(并且
X
不能匹配,因为
Y
X
仅接受模板参数的类型)。

如果将其转换为
X
,则会出现错误,因为
t
未定义(另一个错误是因为
Y
不是模板(按照
X
的要求),它是一个类)。