具有类类型非类型模板参数的类模板成员的类外定义

时间:2019-12-03 19:00:55

标签: c++ templates g++ c++20 non-type

使用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而不是{{ 1}},它可以正常编译:

A

我知道仍在gcc 9上对C ++ 20进行支持,但这是否可行?

1 个答案:

答案 0 :(得分:0)

是的,代码

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

相关问题