静态const double不能具有类内初始化程序。为什么会这样呢? [英] static const double cannot have an in-class initializer. why is it so?

查看:256
本文介绍了静态const double不能具有类内初始化程序。为什么会这样呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The problem 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.

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(){
}


推荐答案

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

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

在C ++中,初始化器是其中的一部分定义的对象。您在类内部为静态成员编写的内容实际上只是一个声明。因此,从形式上来讲,直接在类内部为任何静态成员指定初始化程序是错误的。它与语言的一般声明/定义概念相反。无论您稍后在类中声明的任何静态数据都必须定义

In C++ an initializer is 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 declare inside the class has to be defined later anyway. That's where you will have your chance to specify the initializers.

此规则的一个例外是静态整数常量,因为C ++中的此类常量可以形成整数常量表达式( ICEs)。 ICE在该语言中起着重要作用,为了使它们按预期工作,必须在所有翻译单元中看到积分常数的值。为了使某个常量的值在所有翻译单位中可见,它必须在 declaration 的位置可见。为了实现该语言,可以直接在类中指定初始化程序。

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.

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

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 8 can 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.

整数类型没有类似于ICE的任何功能。而且,硬件平台通常不允许直接将非整数操作数嵌入到机器命令中。因此,上述规则例外不会扩展到非整数类型。只是什么也做不了。

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.

这篇关于静态const double不能具有类内初始化程序。为什么会这样呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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