g++4.0 diagnostic on casting a static const member initializer to a double

Fariborz Jahanian fjahanian@apple.com
Mon Jan 31 19:02:00 GMT 2005


On Jan 31, 2005, at 10:16 AM, Mark Mitchell wrote:

> Fariborz Jahanian wrote:
>> g++ 4.0 in FSF issues a diagnostic on the following test case:
>> template <class T> struct S {
>>   static const double scd = (double)24;
>> };
>> test1.C:2: error: a cast to a type other than an integral or 
>> enumeration type cannot appear in a constant-expression
>> Rewriting the above to:
>> template <class T> struct S {
>>   static const double scd;
>> };
>> template <class T>
>> const double S<T>::scd = (double)24;
>> Compiles. Event though the diagnostic is clear in what it is saying, 
>> but it seems to be a g++ bug
>> one way or the other.
>
> No, the input program is invalid.  From [class.static.data]:
>
> "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(_expr.const_)."

Thanks. Problem here is that the static data member is not of type 
const intergal/enum, so it can not specify
an initializer in its class declaration. A better diagnostic should 
probably mention this.

- fariborz

>
> The diagnostic could be better, but it is indeed accurate; if there's 
> an initializer it must be integral constant-expression, and a cast to 
> "double" cannot appear in such an expression.
>
> -- 
> Mark Mitchell
> CodeSourcery, LLC
> mark@codesourcery.com
> (916) 791-8304



More information about the Gcc mailing list