Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top