C++ static const double 不能有类内初始值设定项。为什么会这样?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13697265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:39:08  来源:igfitidea点击:

static const double cannot have an in-class initializer. why is it so?

c++

提问by sree

The problum with the following code is static member of type "const double" cannot have an in-class initializer. Why is applicable only for a 'const double'in the following code? Please help me.

以下代码的问题是“const double”类型的静态成员不能有类内初始化程序。为什么仅适用于以下代码中的“const double”?请帮我。

class sample{
   static const char mc = '?';
   static const double md = 2.2;
   static const bool mb = true;
};
const char sample::mc;
const double sample::md;
const bool sample::mb;
int main(){
}

回答by AnT

The logic implemented by the C++03 language standard is based on the following rationale.

C++03语言标准实现的逻辑基于以下基本原理。

In C++ an initializeris a part of object definition. What you write inside the class for static members is actually only a declaration. So, formally speaking, specifying initializers for any static members directly inside the class is "incorrect". It is contrary to the general declaration/definition concepts of the language. Whatever static data you declareinside the class has to be definedlater anyway. That's where you will have your chance to specify the initializers.

在 C++ 中,初始化程序是对象定义的一部分。您在类中为静态成员编写的内容实际上只是一个声明。因此,正式地说,直接在类内部为任何静态成员指定初始值设定项是“不正确的”。它与语言的一般声明/定义概念相反。无论如何,您在类中声明的任何静态数据都必须稍后定义。这就是您将有机会指定初始值设定项的地方。

An exception from this rule was made for static integer constants, because such constants in C++ can form Integral Constant Expressions (ICEs). ICEs play an important role in the language, and in order for them to work as intended the values of integral constants have to be visible in all translation units. In order to make the value of some constant visible in all translation units, it has to be visible at the point of declaration. To achieve that the language allows specifying the initializer directly in class.

此规则的一个例外是静态整数常量,因为 C++ 中的此类常量可以形成积分常量表达式 (ICE)。ICE 在语言中扮演着重要的角色,为了让它们按预期工作,积分常数的值必须在所有翻译单元中可见。为了使某些常量的值在所有翻译单元中可见,它必须在声明点可见。为了实现这一点,该语言允许直接在类中指定初始化程序。

Additionally, on many hardware platforms constant integer operands can be embedded directly into the machine commands. Or the constant can be completely eliminated or replaced (like, for example, multiplication by 8can be implemented as a shift by 3). In order to facilitate generation of machine code with embedded operands and/or various arithmetical optimizations it is important to have the values of integral constants visible in all translation units.

此外,在许多硬件平台上,常量整数操作数可以直接嵌入到机器命令中。或者可以完全消除或替换常数(例如,乘法8可以实现为移位3)。为了便于生成带有嵌入式操作数和/或各种算术优化的机器代码,重要的是在所有翻译单元中都可以看到整数常量的值。

Non-integral types do not have any functionality similar to ICE. Also, hardware platforms do not normally allow embedding non-integral operands directly into the machine commands. For this reason the above "exception from the rules" does not extend to non-integral types. It would simply achieve nothing.

非整数类型没有任何类似于 ICE 的功能。此外,硬件平台通常不允许将非整数操作数直接嵌入到机器命令中。出于这个原因,上述“规则的例外”并没有扩展到非整数类型。它只会一无所获。

回答by Luchian Grigore

Pre-C++11, only constintegral typescould be directly initialized in the class definition. It's just a restriction imposed by the standard.

在 C++11 之前,只有const整数类型可以在类定义中直接初始化。这只是标准强加的限制。

With C++11, this no longer applies.

对于 C++11,这不再适用。

回答by anton_rh

The compiler offered me to use constexprinstead of const:

编译器让我使用constexpr而不是const

static_consts.cpp:3:29: error: ‘constexpr' needed for in-class initialization of static data member ‘const double sample::md' of non-integral type [-fpermissive]
static_consts.cpp:7:22: error: ‘constexpr' needed for in-class initialization of static data member ‘const double sample::md' of non-integral type [-fpermissive]

I've just accepted the offer:

我刚刚接受了这个提议:

class sample{
   static const char mc = '?';
   static constexpr double md = 2.2;
   static const bool mb = true;
};
const char sample::mc;
const bool sample::mb;
int main(){
}

And now it compiles just fine (C++11).

现在它编译得很好(C++11)。