Mahesh
Mahesh

Reputation: 34625

typedef - Primitive type to primitive type

Is it valid C++ to have a typedef for a primitive type to another primitive type ?

typedef int long;

On VS 2012, warning is issued but compiles fine.

warning C4091: 'typedef ' : ignored on left of 'long' when no variable is declared

But on gcc-4.3.4, it fails.

error: declaration doesnot declare anything.

Which compiler is standard conformant ?

PS: I won't write something like this in production code. Just came up with the thought and checking.

Upvotes: 1

Views: 665

Answers (3)

Pete Becker
Pete Becker

Reputation: 76360

Both do what the standard requires. That typedef is not valid, and both compilers issue a diagnostic.

Upvotes: 2

Cat Plus Plus
Cat Plus Plus

Reputation: 129814

Is it valid C++

No. C++11, § 7.1.3.6:

In a given scope, a typedef specifier shall not be used to redefine the name of any type declared in that scope to refer to a different type.

Upvotes: 7

They're both saying the same thing, but one reports it as an error. Note that the VS warning says "typedef was ignored." The thing is that int long and long int are synonyms, so you're basically creating an unnamed typedef to a long.

Upvotes: 5

Related Questions