1

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.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • 2
    Not sure but this seems like a horrible idea to me. The first problem is the one you've identified already. Beyond that, any new programmers that come to your project will likely be fooled looking at the code. And what happens when if the code ever needs to be ported to a system where int means something different (larger, for example)? – Jonathan Wood Nov 30 '12 at 16:26
  • @JonathanWood I understand but just checking the language rules here. I won't write something like this. – Mahesh Nov 30 '12 at 16:28

3 Answers3

7

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.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • 1
    I believe it's valid C++, actually. It's not parsed as a typedef of `int` to `long`, but as a typedef of `int long` (which is the same as `long int` or just `long`) specifying no name to give to the type. It would be invalid if the example was e.g. `typedef int char;` instead. – Angew is no longer proud of SO Nov 30 '12 at 16:32
  • While what the OP is trying to accomplish is not possible for the reason you cite, it should also be noted that it is not the reason for the error/warning received. The error/warning is because "int long" is interpreted as single type so the typedef has no effect. VC++ warns and keeps going in this case. GCC is more strict. Which is more correct -- who knows. It is probably an implementation detail left to the compiler. – Sean Cline Nov 30 '12 at 16:33
  • @Cat Plus Plus Why can't the rule be interpreted this way (http://ideone.com/FbB7ZY)? – Mahesh Nov 30 '12 at 16:35
  • @SeanCline - Re *Which is more correct* - Both are. The standard only requires that a conforming implementation must issue at least one diagnostic message if a program contains a violation of any diagnosable rule. What the implementation does beyond this is up to the implementation. It could stop right then and there; ignore the problem and continue; somehow read the programmers mind, fix the problem, and continue; etc. Most compilers try to do the mind reading trick so they can catch multiple errors in one shot. (They also tend to skip creating an executable when errors are detected.) – David Hammen Nov 30 '12 at 16:59
  • 1
    @Angew: It's still invalid, since a `typedef` must declare a type name. C++11, 7/3: "In a *simple-declaration*, the optional *init-declarator-list* can be omitted only when declaring a class or enumeration" (the *init-declarator-list* being the part that includes the identifier(s) being declared). – Mike Seymour Nov 30 '12 at 17:04
  • @MikeSeymour Ah, OK. Thanks for the correction; I apparently didn't dig into the standard deep enough. – Angew is no longer proud of SO Nov 30 '12 at 17:19
  • @Mahesh That code has no relation to this issue whatsoever. It's two different typedefs. – Cat Plus Plus Dec 01 '12 at 07:31
5

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.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
2

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

Pete Becker
  • 74,985
  • 8
  • 76
  • 165