[英]Out-of-class definition of member of a class template with a class-type non-type template parameter
使用 C++20,可以定义一个带有类类型非类型模板参数的模板类:
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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.