为什么GCC认为constexpr静态数据成员的定义必须标记为constexpr? [英] Why does GCC think that the definition of a constexpr static data member must be marked constexpr?

查看:940
本文介绍了为什么GCC认为constexpr静态数据成员的定义必须标记为constexpr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


[C ++ 14:7.1.5 / 1]: constexpr 说明符只应用于变量或变量模板的定义,函数或函数模板的声明,或文字类型的静态数据成员的声明(3.9)。如果函数,函数模板或变量模板的任何声明具有 constexpr 说明符,则其所有声明应包含 constexpr 说明符。 [..]

[C++14: 7.1.5/1]: The constexpr specifier shall be applied only to the definition of a variable or variable template, the declaration of a function or function template, or the declaration of a static data member of a literal type (3.9). If any declaration of a function, function template, or variable template has a constexpr specifier, then all its declarations shall contain the constexpr specifier. [..]

请注意,第二句没有提及静态数据成员因此这一段中没有要求所有的声明(这里我正在考虑一个定义的声明) constexpr static 数据成员具有 constexpr 说明符。

Notice that the second sentence does not mention "a static data member" the way the first sentence does, so there is no requirement in this passage that all declarations (and here I'm considering a defining declaration specifically) of a constexpr static data member have the constexpr specifier.

我找不到规则

为什么GCC会拒绝接受以下程序?

Why, then, does GCC reject the following program?

#include <chrono>

using namespace std::chrono_literals;

#define DUR 1000ms

struct T
{
   static constexpr auto dur_1 = DUR;
};

decltype(T::dur_1) T::dur_1;

// main.cpp:12:23: error: 'constexpr' needed for in-class initialization of static data member 'const std::chrono::duration<long int, std::ratio<1l, 1000l> T::dur_1' of non-integral type [-fpermissive] 
// decltype(T::dur_1) T::dur_1;
//                       ^


推荐答案

我,我没有看到明确的要求,但我们可以看到为什么它是一个问题从缺陷报告699:必须在类成员规范中定义constexpr成员函数吗? ,其中虽然处理constexpr成员函数说明如下(强调我):

This looks underspecified to me, I don't see an explicit requirement but we can see why it is an issue from defect report 699: Must constexpr member functions be defined in the class member-specification? which although dealing with constexpr member functions says the following (emphasis mine):


禁止放宽允许单独声明和
定义constexpr成员函数,一些问题需要回答
,例如constexpr说明符是否必须出现在
声明和定义(内联说明符不需要)。如果
可以在一个或另一个省略,有一个可用性问题
关于constexpr意味着const的事实;需要在省略
constexpr的声明中明确指定const限定符

虽然在这种情况下添加 const 不能解决问题,虽然在一个更简单的情况下它似乎解决了问题。我们可以看到在一个更简单的情况下,clang和gcc都需要const或constexpr:

Although in this case adding const does not solve the problem although in a simpler cases it does seem to solve the issue. We can see in a simpler case both clang and gcc require either const or constexpr:

struct T
{
   static constexpr int blah = 1 ;
};

const int T::blah ;

更新

此gcc错误报告: Bogus错误:重新声明...在constexpr中有所不同'具有以下来自Richard Smith的引用:

This gcc bug report: Bogus "error: redeclaration ... differs in ‘constexpr’" has the following quote from Richard Smith:


没有规则要求对
连续声明变量同意在constexpr'ness(此规则仅适用于函数)。

There is no rule requiring successive declarations of variables to agree in 'constexpr'ness (this rule only applies to functions).

这看起来像一个gcc错误,就像它可以在标准中使用一些清晰。

So this looks like a gcc bug, although it still seems like it could use some clarity in the standard.

这篇关于为什么GCC认为constexpr静态数据成员的定义必须标记为constexpr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆