• <i id='S1DlK'><tr id='S1DlK'><dt id='S1DlK'><q id='S1DlK'><span id='S1DlK'><b id='S1DlK'><form id='S1DlK'><ins id='S1DlK'></ins><ul id='S1DlK'></ul><sub id='S1DlK'></sub></form><legend id='S1DlK'></legend><bdo id='S1DlK'><pre id='S1DlK'><center id='S1DlK'></center></pre></bdo></b><th id='S1DlK'></th></span></q></dt></tr></i><div id='S1DlK'><tfoot id='S1DlK'></tfoot><dl id='S1DlK'><fieldset id='S1DlK'></fieldset></dl></div>

    1. <tfoot id='S1DlK'></tfoot>

      <small id='S1DlK'></small><noframes id='S1DlK'>

      <legend id='S1DlK'><style id='S1DlK'><dir id='S1DlK'><q id='S1DlK'></q></dir></style></legend>

        • <bdo id='S1DlK'></bdo><ul id='S1DlK'></ul>

        为什么不允许静态常量浮点数?

        Why aren#39;t static const floats allowed?(为什么不允许静态常量浮点数?)
        <tfoot id='vjhNE'></tfoot>

        <small id='vjhNE'></small><noframes id='vjhNE'>

              <tbody id='vjhNE'></tbody>
            <i id='vjhNE'><tr id='vjhNE'><dt id='vjhNE'><q id='vjhNE'><span id='vjhNE'><b id='vjhNE'><form id='vjhNE'><ins id='vjhNE'></ins><ul id='vjhNE'></ul><sub id='vjhNE'></sub></form><legend id='vjhNE'></legend><bdo id='vjhNE'><pre id='vjhNE'><center id='vjhNE'></center></pre></bdo></b><th id='vjhNE'></th></span></q></dt></tr></i><div id='vjhNE'><tfoot id='vjhNE'></tfoot><dl id='vjhNE'><fieldset id='vjhNE'></fieldset></dl></div>
                • <bdo id='vjhNE'></bdo><ul id='vjhNE'></ul>
                  <legend id='vjhNE'><style id='vjhNE'><dir id='vjhNE'><q id='vjhNE'></q></dir></style></legend>

                  本文介绍了为什么不允许静态常量浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个类,它本质上只是包含一堆在我的应用程序中使用的常量定义.但出于某种原因,long 可以编译但 float 不能:

                  I have a class which is essentially just holds a bunch of constant definitions used through my application. For some reason though, longs compile but floats do not:

                  class MY_CONSTS
                  {
                  public :
                      static const long   LONG_CONST = 1;      // Compiles 
                      static const float FLOAT_CONST = 0.001f; // C2864
                  };
                  

                  出现以下错误:

                  1>c:projectsmyprojectConstant_definitions.h(71) : error C2864: 'MY_CONSTS::FLOAT_CONST' : only static const integral data members can be initialized within a class
                  

                  我错过了什么吗?

                  推荐答案

                  回答你提出的实际问题:因为标准是这么说的".

                  To answer the actual question you asked: "because the standard says so".

                  只有静态、常量、整型 类型(包括枚举)的变量可以在类声明中初始化.如果编译器支持浮点数的内联初始化,则它是一个扩展.正如其他人指出的那样,处理静态、常量、非整型变量的方法是在类的相应源文件(而不是头文件)中定义和初始化它们.

                  Only variables of static, constant, integral types (including enumerations) may be initialized inside of a class declaration. If a compiler supports in-line initialization of floats, it is an extension. As others pointed out, the way to deal with static, constant, non-integral variables is to define and initialize them in the class's corresponding source file (not the header).

                  C++ 标准第 9.2 节类成员"第 4 项:

                  C++ Standard Section 9.2 "Class Members" item 4:

                  member-declarator 可以包含一个constant-initializer 仅当它声明了一个静态成员(9.4)const 积分或 const 枚举类型,见 9.4.2.

                  A member-declarator can contain a constant-initializer only if it declares a static member (9.4) of const integral or const enumeration type, see 9.4.2.

                  第 9.4.2 节静态数据成员"第 2 项:

                  Section 9.4.2 "Static Data Members" item 2:

                  如果静态数据成员是 const整数或常量枚举类型,它在类定义中的声明可以指定一个常量初始化器这应该是一个积分常数表达式 (5.19).在这种情况下,成员可以出现在积分常数中表达式.该成员仍将是如果它是在命名空间范围中定义的在程序和命名空间中使用范围定义不应包含初始化器.

                  If a static data member is of const integral or const enumeration type, its declaration in the class denition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be dened in a namespace scope if it is used in the program and the namespace scope denition shall not contain an initializer.

                  这篇关于为什么不允许静态常量浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                    <bdo id='h54Ud'></bdo><ul id='h54Ud'></ul>
                  • <tfoot id='h54Ud'></tfoot>
                      <tbody id='h54Ud'></tbody>
                    <i id='h54Ud'><tr id='h54Ud'><dt id='h54Ud'><q id='h54Ud'><span id='h54Ud'><b id='h54Ud'><form id='h54Ud'><ins id='h54Ud'></ins><ul id='h54Ud'></ul><sub id='h54Ud'></sub></form><legend id='h54Ud'></legend><bdo id='h54Ud'><pre id='h54Ud'><center id='h54Ud'></center></pre></bdo></b><th id='h54Ud'></th></span></q></dt></tr></i><div id='h54Ud'><tfoot id='h54Ud'></tfoot><dl id='h54Ud'><fieldset id='h54Ud'></fieldset></dl></div>

                        <small id='h54Ud'></small><noframes id='h54Ud'>

                          <legend id='h54Ud'><style id='h54Ud'><dir id='h54Ud'><q id='h54Ud'></q></dir></style></legend>