constexpr定义被声明为const的文字类型的静态数据成员 [英] constexpr defining static data member of literal type that is declared const

查看:535
本文介绍了constexpr定义被声明为const的文字类型的静态数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于constexpr的问题,它定义了一个在类定义中声明为const(并且未指定inline或constexpr)的文字类型的静态数据成员:



/ / Sh

  struct S 
{
static int const i; //未指定inline或constexpr
};

// S.cpp

  #includeSh
constexpr int const S :: i = 42;

// main.cpp

  #includeSh
int main()
{
return S :: i;
}

Clang / gcc在C ++ 11/14模式下返回42,但报告在C ++ 17模式下出错(对S :: i的未定义引用)。如果我注释掉constexpr在C ++ 17模式下都返回42。
$ b $ s :: i有外部链接,因为S有外部链接。 S :: i没有被声明为constexpr,所以(如果我没有弄错)C ++ 17 10.1.5 p1不适用:

lockquote

使用constexpr
说明符声明的函数或静态数据成员隐式为内联函数或变量


我理解这一点句子,就好像它的意思(大胆我的理解):
在类定义中使用constexpr说明符声明的静态数据成员隐式为内联变量

S :: i因此不是内联变量。
但是S :: i的定义似乎在C ++ 17模式下具有内部链接,就好像constexpr意味着内联。它是否正确?如果是的话,标准证明在哪里?

或者我误解了10.1.5 p1,它确实意味着(大胆的误解):
静态数据在类定义中使用constexpr说明符声明的成员,并且名称空间范围中的定义隐含地是一个内联变量?

解决方案


然而,S :: i的定义似乎在C ++ 17
模式中具有内部链接,就好像constexpr意味着内联。它是否正确?如果是的话,标准中的
证明在哪里?

是的,这是正确的。 cppreference


当在具有静态存储持续时间(静态类成员或命名空间范围
变量)的变量
的decl-specifier-seq中使用内联说明符时,声明变量作为内联变量。


$ b 声明constexpr的静态成员变量(但不是名称空间范围变量)隐含为内联变量。 (自C ++ 17以来)



I have a question about constexpr defining a static data member of literal type that is declared const (and not specified inline or constexpr) in the class definition:

// S.h

struct S
{
  static int const i; // not specified inline or constexpr
};

// S.cpp

#include "S.h"
constexpr int const S::i = 42; // definition, not declaration

// main.cpp

#include "S.h"
int main()
{
  return S::i;
}

Clang/gcc return 42 in C++11/14 mode, but report an error (undefined reference to S::i) in C++17 mode. If I comment out constexpr both return 42 in C++17 mode, too.

S::i has external linkage because S has external linkage. S::i is not declared constexpr and so (if I'm not mistaken) C++17 10.1.5 p1 does not apply:

A function or static data member declared with the constexpr specifier is implicitly an inline function or variable

I understand this sentence as if it means (bold my understanding): A static data member declared with the constexpr specifier in the class definition is implicitly an inline variable

S::i is thus not an inline variable. Yet the definition of S::i seems to have internal linkage in C++17 mode as if constexpr means inline. Is this correct? If so where is the proof in the standard?

Or do I misunderstand 10.1.5 p1 and it really means (bold my misunderstanding): A static data member declared with the constexpr specifier in the class definition and the definition in namespace scope is implicitly an inline variable?

解决方案

Yet the definition of S::i seems to have internal linkage in C++17 mode as if constexpr means inline. Is this correct? If so where is the proof in the standard?

Yes, it is correct. cppreference :

The inline specifier, when used in a decl-specifier-seq of a variable with static storage duration (static class member or namespace-scope variable), declares the variable to be an inline variable.

A static member variable (but not a namespace-scope variable) declared constexpr is implicitly an inline variable. (since C++17)

这篇关于constexpr定义被声明为const的文字类型的静态数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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