使用 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() {}
我知道对 C++20 的支持在 gcc 9 上仍处于试验阶段,但这是否可能?
最佳答案
是的,代码
template <auto a>
struct B {
void f();
};
template <auto a>
void B<a>::f() {}
将在 C++20 中编译。请注意
编码
#include <type_traits>
template<typename T>
concept A = std::is_same<T,int>::value;
template <A a>
struct B {
void f();
};
template <A a>
void B<a>::f() {}
也将在 C++20 中编译,因为 A 是
concept
.
关于c++ - 具有类类型非类型模板参数的类模板成员的类外定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59163716/