views:

347

answers:

3

Why is this code invalid?

typedef int INT;
unsigned INT a=6;

whereas the following code is valid

typedef int INT;
static INT a=1; 

?

As per my understanding unsigned int is not a "simple type specifier" and so the code is ill-formed. I am not sure though.

Can anyone point to the relevant section of the Standard which makes the first code invalid(and the second code valid)?

EDIT

Although Johannes Schaub's answer seemed to be correct and to the point(he had deleted his answer BTW) I accepted James Curran's answer for its correctness and preciseness.

+11  A: 
  • 7.1.1 : static is a storage class specifier. It can be placed before any type.
  • 7.1.5 : what is a type specifier (unsigned can be combined with char, long, short, or int)
Scharron
`unsigned` can also be a type all by itself.
torak
And qualifies implicitely int type
Scharron
+1 not quite elaborative, but correct and if he's got any knowledge of standardese, i suspect these section numbers will help him.
Johannes Schaub - litb
@Johannes: Yes his answer is correct but I have accepted James' answer because his answer is elaborative as well. BTW why did you delete your answer? :)
Prasoon Saurav
+26  A: 

typedefs are not like macros. They are not just text substitution. A Typedef creates a new typename.

Now when you say unsigned int, the unsigned isn't a modifier which is tacked onto the int. unsigned int is the complete typename; it just happens to have a space in it.

So, when you say typedef int INT; then INT is the complete typename. It can't be modified.

static (like const) is a storage class specifier. It's not actually part of the type name.

James Curran
C++0x standard section 3.9.1 paragraph 3 lists the unsigned integral types and supports your answer. `unsigned int` is a type that has a space in the name. `unsigned` is not listed as a modifier to another type to magically make it non-negative.
Kristo
+2  A: 

Don't forget that typedef-ing is not like macro-defining; in your example, it seems like if you think your INT should be seen like a literal int. From the compiler point of view, typedef defines type-aliases, but this is not seen at "syntax" level (typedef-ed types are like "native" types at the syntax level); and since at that level unsigned is allowed before char long short or int only, your unsigned INT is seen like a "type" ("different" from char, long, short, int) preceded by unsigned.

ShinTakezou