考虑以下几点:使用可变参数模板的一个特例用作模板参数
template <class...>
struct MyT;
template <class T>
struct MyT<T> {};
template <template <class> class TT = MyT> struct A {}; // fine
using B = A<MyT>; // does not compile
int main() {
return 0;
}
当MyT
被用作A
默认参数时,编译器(克++ 5.4.0)是高兴。然而,当它被用于实例A
,故事不同的是:
temp.cpp:19:16: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class> class TT> struct A’
using B = A<MyT>;
^
temp.cpp:19:16: note: expected a template of type ‘template<class> class TT’, got ‘template<class ...> struct MyT’
我可以通过引入一个别名解决它:
template <class T>
using MyTT = MyT<T>;
using B = A<MyTT>; // fine
的问题:什么是错误的原因是有没有引入别名的解决方案?
编辑请注意,A
被声明为具有所示的模板模板参数,并且不会被更改。
使用'template class TT = MyT> struct A {};'。在这种情况下没有考虑到专业化,因此定义不同。 – skypjack
@skypjack“A”的声明是给定的。我应该说明这一点。 – AlwaysLearning
然后你应该定义一个模板类或别名声明,它只需要一个参数。 – skypjack