Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
c++ - Bug: dmc doesn't like static const double
The following code gives an error in spite of c being const. class A { static const double c = 1.0; }; static const double c = 1.0; ^ te.cpp(5) : Error: initializer for non-const static member must be outside of class def --- errorlevel 1 Aug 31 2007
I've fallen into this trap as well on more than one occasion, but actually DMC is just following the standard here (you will see similar behaviour using compilers with the EDG front-end). You can only do what you are trying to do for integral or enumeration types. 9.4.2 Static data members [class.static.data] - item 4 says: If a static data member is of const integral or const enumeration type, its declaration in the class definition 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 defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer. Sz. Horvát wrote:The following code gives an error in spite of c being const. class A { static const double c = 1.0; }; static const double c = 1.0; ^ te.cpp(5) : Error: initializer for non-const static member must be outside of class def --- errorlevel 1 Aug 31 2007
Anton Sekeris wrote:I've fallen into this trap as well on more than one occasion, but actually DMC is just following the standard here (you will see similar behaviour using compilers with the EDG front-end). You can only do what you are trying to do for integral or enumeration types. 9.4.2 Static data members [class.static.data] - item 4 says: If a static data member is of const integral or const enumeration type, its declaration in the class definition 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 defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer. Aug 31 2007
In that case, the member can appear in integral constant expressions. Aug 31 2007
Sz. Horvát wrote:>> In that case, the member can appear in integral constantexpressions. Sep 01 2007
Sz. Horvát wrote:But I really cannot see the reason for not allowing this, it just makes one's life more difficult ... did the people writing the standard simply forget about floating point types? Aug 31 2007
Sz. Horvát wrote:The following code gives an error in spite of c being const. class A { static const double c = 1.0; }; static const double c = 1.0; ^ te.cpp(5) : Error: initializer for non-const static member must be outside of class def --- errorlevel 1 Sep 01 2007
|