定义同一类型的模板类的静态constexpr成员

发布于 2025-02-01 21:39:18 字数 1340 浏览 1 评论 0 原文

模板类

对于模板类,

template <typename T>
struct Test {
    T data;

    static const Test constant;
};

定义 static constexpr 专用类型的成员变量是可以的:

template <>
inline constexpr Test<int> Test<int>::constant {42};

https://godbolt.org/z/o4c4yojmf


虽然在定义 statation constancpr 直接从模板类中直接从模板类中定义 static constancpr 时,编译器会差异。

template <typename T>
inline constexpr Test<T> Test<T>::constant {42};

https://godbolt.org/z/m8jdx3wzm

编译。
clang 忽略 constexpr 定义中的指定符。
msvc ... /std:C ++ 17 效果很好,但是/std:C ++ 20 由于重新定义而拒绝。


我了解到 constexpr 可以应用对于定义,变量或变量模板

在此示例中,哪一个是对的?为什么?

A similar question of non-templated class

For a template class,

template <typename T>
struct Test {
    T data;

    static const Test constant;
};

it's fine when defining a static constexpr member variable of specialized type:

template <>
inline constexpr Test<int> Test<int>::constant {42};

https://godbolt.org/z/o4c4YojMf


While results are diverged by compilers when defining a static constexpr member directly from a template class without instantiation:

template <typename T>
inline constexpr Test<T> Test<T>::constant {42};

https://godbolt.org/z/M8jdx3WzM

GCC compiles.
clang ignores the constexpr specifier in definition.
MSVC... /std:c++17 works well, but /std:c++20 rejects due to redefinition.


I have learnt that constexpr can be applied to the definition a variable or variable template

And in this example, which one is right? Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2025-02-08 21:39:18

我认为gcc在 static 数据成员名为常数普通数据成员变量(尽管它仍然被视为模板的实体)。

并且由于常数是普通数据成员变量“> dcl.constexpr#1.Sessence-1 适用于此:

constexpr规范应仅应用于变量或变量模板的定义或函数或函数模板的声明。

(强调地雷)

因此,作为您在类模板之后提供的构造,只是普通静态数据成员 constance>常数,给定的示例是良好的。

template <typename T>
constexpr Test<T> Test<T>::constant {42};  //this is an out-of-class definition for the ordinary static data member variable `constant`

注意,不提及模板实体,而是仅提及“变量”和“变量模板”,并且由于 constant 是一个变量(普通),因此应适用于 constand> constant

I think GCC is correct in accepting the given example. This is because the static data member named constant is an ordinary data member variable(although it is still considered a templated entity).

And since constant is an ordinary data member variable, dcl.constexpr#1.sentence-1 is applicable to it:

The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template.

(emphasis mine)

Thus, as the construct that you have provided after the class template is nothing but an out-of-class definition for the ordinary static data member constant, the given example is well-formed.

template <typename T>
constexpr Test<T> Test<T>::constant {42};  //this is an out-of-class definition for the ordinary static data member variable `constant`

Note

Note that dcl.constexpr#1.sentence-1 doesn't mention templated entity, but instead only mention "variable" and "variable template" and since constant is a variable(ordinary), the same should be applicable to constant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文