【问题标题】:Out-of-class definition of member of a class template with a class-type non-type template parameter具有类类型非类型模板参数的类模板成员的类外定义
【发布时间】: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


【解决方案1】:

是的,代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2022-01-20
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多