Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates_Template Specialization_Template Templates_Template Aliases - Fatal编程技术网

C++ 模板参数和模板别名:编译器错误?

C++ 模板参数和模板别名:编译器错误?,c++,templates,template-specialization,template-templates,template-aliases,C++,Templates,Template Specialization,Template Templates,Template Aliases,我对以下具有代表性的示例代码有问题: template<int I> struct X {}; template<int I> struct Y {}; template<int I> struct XX: X<I> {}; template<int I> struct YY: Y<I> {}; template<template<int> class TP> struct traits;

我对以下具有代表性的示例代码有问题:

template<int I> struct X {}; template<int I> struct Y {}; template<int I> struct XX: X<I> {}; template<int I> struct YY: Y<I> {}; template<template<int> class TP> struct traits; template<> struct traits<X> { template<int I> using Tpl=XX<I>; }; template<> struct traits<Y> { template<int I> using Tpl=YY<I>; }; template<template<int> class TP> struct Z {}; template<template<int> class TP> struct W: Z<traits<TP>::Tpl> {}; int main() { Z<traits<X>::Tpl> zx; Z<traits<Y>::Tpl> zy; W<X> wx; W<Y> wy; return 1; } 模板 结构X{}; 模板 结构Y{}; 模板 结构XX:X{}; 模板 结构YY:Y{}; 模板 结构特征; 模板 结构特征{ 模板 使用Tpl=XX; }; 模板 结构特征{ 模板 使用Tpl=YY; }; 模板 结构Z{}; 模板 结构W:Z{}; int main(){ zzx; Z-zy; W-wx; W wy; 返回1; } 此代码可以使用icc-19.0.0进行良好编译(似乎可以使用msvc-19.24进行编译),但无法使用gcc-10.1、clang-10.0.0和icc-18.0.0进行编译

对于gcc-10.1,错误消息为:

<source>:32:28: error: type/value mismatch at argument 1 in template parameter list for 'template<template<int <anonymous> > class TP> struct Z'

   32 | struct W: Z<traits<TP>::Tpl> {};

      |                            ^

<source>:32:28: note:   expected a class template, got 'traits<TP>::Tpl'
:32:28:错误:“模板结构Z”的模板参数列表中参数1的类型/值不匹配
32 |结构W:Z{};
|                            ^
:32:28:注意:应为类模板,获得'traits::Tpl'
上下文:我有一个模板类
Z
,它有一个模板参数。我想从中派生一个类,
W
,对于最终用户来说,它接受与
Z
相同的模板参数(
X
Y
),但将它们分派到内部类
XX
YY
,通过从它们派生来修改
X
Y
的行为

这个问题是编译器错误吗?如果是,是否有某种解决方法


非常感谢

您需要告诉编译器
Tpl
是一个模板:

template<template<int> class TP>
struct W: Z<traits<TP>::template Tpl> {};
                   // ^^      
模板
结构W:Z{};
// ^^      
好吧,这方面没有什么变化,我可能是因为一些编译器对它比较宽松,让你不用它也能逃脱。
需要它的原因类似于类型需要
typename
的原因:可能存在
traits
的专门化,其中
Tpl
不是一个模板,因此您需要告诉编译器它确实是一个模板。

您需要告诉编译器
Tpl
是一个模板:

template<template<int> class TP>
struct W: Z<traits<TP>::template Tpl> {};
                   // ^^      
模板
结构W:Z{};
// ^^      
好吧,这方面没有什么变化,我可能是因为一些编译器对它比较宽松,让你不用它也能逃脱。
需要它的原因类似于类型需要
typename
的原因:可能存在
traits
的专门化,其中
Tpl
不是模板,因此您需要告诉编译器它确实是一个模板。

非常类似的问题:我们不确定它是否符合重复的条件,再想一想,它确实有……非常相似的问题:我不确定它是否符合复制品的条件,再想一想,它确实有。。。