CC认为constexpr静态数据成员的定义必须标记为const

CC认为constexpr静态数据成员的定义必须标记为const

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

问题描述

请注意,第二句没有提及静态数据成员因此这一段中没有要求所有的声明(这里我正在考虑一个定义的声明) 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;
//                       ^


推荐答案

我,我没有看到明确的要求,但我们可以看到为什么它是一个问题从,其中虽然处理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):

虽然在这种情况下添加 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错误报告:具有以下来自Richard Smith的引用:

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

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

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

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

08-22 14:38