【发布时间】:2020-03-28 12:42:39
【问题描述】:
使用 C++20,可以定义一个采用 class-type non-type template parameter 的模板类:
struct A {};
template <A a>
struct B {
void f();
};
但是是否可以像整数类型一样定义类外的B::f()?因为这个
template <int>
struct C {
void f();
};
template <int i>
void C<i>::f() {}
编译,但是这个
template <A a>
void B<a>::f() {}
当我尝试在 gcc 9 上编译它时产生“无效使用不完整类型”错误。奇怪的是,如果我替换 B 以采用非类型参数 auto 而不是 A,它会编译很好:
template <auto a>
struct B {
void f();
};
template <auto a>
void B<a>::f() {}
我知道在 gcc 9 上对 C++20 的支持仍处于试验阶段,但这是否应该可行?
【问题讨论】:
-
该术语是“非类型模板参数”而不是“模板参数对象”。是的,这应该有效,提交92776。
-
当,那是快 lmao。更准确地说是“类类型的非类型模板参数”,我以为“模板参数对象”是同义词……
-
@SepiaColor:类类型的非类型模板参数有有个模板参数对象,但它们与参数本身不同,参数本身对于所有专业化都抽象存在一次。
标签: c++ templates g++ c++20 non-type