error: `.' cannot appear in a constant-expression in gcc 3.4.2 version

Brian Dessent
Fri Dec 9 16:20:00 GMT 2005 wrote:

> I am using a switch case statement in my code. The case condition checks
> for a "class object.value".
> This is throwing an error saying
> error: `.' cannot appear in a constant-expression
> The same code works fine in other versions of gcc (i.e. 2.96 ). But I
> encounter this problem in gcc 3.4.2 version.
> Can u help me with a fix for this issue?

The C++ standard 5.19.1 says:

> In several places, C++ requires expressions that evaluate to an integral 
> or enumeration constant: as array bounds (8.3.4, 5.3.4), as case 
> expressions (6.4.2), as bitfield lengths (9.6), as enumerator 
> initializers (7.2), as static member initializers (9.4.2), and as 
> integral or enumeration nontype template arguments (14.3).
> 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), nontype 
> 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. Only type conversions to integral or 
> enumeration types can be used. In particular, except in sizeof 
> expressions, functions, class objects, pointers, or references shall not 
> be used, and assignment, increment, decrement, functioncall, or comma 
> operators shall not be used.

So basically what you're trying to do is not allowed by the standard,
and you were just lucky that gcc 2.x accepted it.  The reason is that a
switch statement is usually implemented with a jump table, and so the
values of each case label must be of integer type and the compiler must
be able to determine them at compile-time.  To support this ability to
implement switches with jump tables, the standard forbids class objects
from being used.


More information about the Gcc-help mailing list