Typo at msdn page “C++ Constant Expressions”?

我的未来我决定 提交于 2019-12-07 12:39:53

问题


It says at msdn page for c++ constant expressions that:

Nonintegral constants must be converted (either explicitly or implicitly) to integral types to be legal in a constant expression. Therefore, the following code is legal:

const double Size = 11.0;
char chArray[(int)Size];

At least on VC++ 10.0 the second line produces: "error C2057: expected constant expression". So is it legal on some other compiler or is the msdn page simply wrong?


回答1:


According to 5.19/1 :

An integral constant-expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof expressions. Floating literals (2.13.3) can appear only if they are cast to integral or enumeration types.

From my understanding the code is invalid, while the following is legal :

char chArray[(int)11.0];



回答2:


That's not legal according to Standard C++. See 5.19/2 for the rules in the spec.



来源:https://stackoverflow.com/questions/4518787/typo-at-msdn-page-c-constant-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!