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

标签 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 的非类型参数而不是 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/

相关文章:

c++ - 如何从 C++ 中的二进制数据包中提取 GUID?

c++ - 在定义处初始化模板结构

c++ - 对类模板成员函数的 undefined reference

c++ - 内存布局问题

c++ - g++中 `main( )`的链接是什么

具有任意索引的数组的 C++ 类

c++ - 变量自动分配

c++ - 如何存储旧的 txt 行并将新输出存储到 iostream 上的新行 (C++)

c++ - 在派生类中绑定(bind)非静态模板化成员函数

c++ - 如何使用 -Wall 启用 -Wunused-variable?