我有一个类的多个别名,并希望这些别名继承基类的默认模板参数。这是我试图实现的语法的简单示例:
template<int f = 5>
class A {};
template<int T/*= 5*/>
using Test = A<T>;
int main()
{
A<> foo;
Test<> foo2; // error: wrong number of template arguments (0, should be 1)
}
或者我是否必须使默认值成为显式可访问的值?
static const int DefaultVal = 5;
template<int f = DefaultVal>
class A {};
template<int T= DefaultVal>
using Test = A<T>;
最佳答案
您不能“继承”默认值。
另一种可能性是使用可变参数模板:
template <int ... Ts>
using Test = A<Ts...>;
这允许
Test<>
作为 A<>
所以 A<5>
。但它“撒谎”了无效的
Test<1, 2, 3, 4>
。关于c++ - 继承默认模板值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58085535/