13

The old familiar one:

typedef int cute_int; //cute : common and familiar syntax. 

This syntax is perfect. No problem.

Now, when we can write typedefs like above, then what is the point of allowing this syntax:

int typedef crazy_int; //crazy : uncommon and unfamiliar syntax.

Just to confuse programmers? Is this syntax even needed anywhere (when in fact we've the previous one)? What do you think from the compilers' point of view? Do they find it cute or crazy? Or it doesn't matter to the compilers at all?


By the way, this code came from here : Use of typename keyword with typedef and new

In case if you're wondering if that is syntax error, then check out the working code here at ideone.

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 4
    C++/C were pretty much designed to confuse programmers. It was a clear design goal to maximise the complexity of the expressions that you can write - upside is you can write crazily complex code in loads of different ways - downside is you can write crazily complex code in loads of different ways. – James Gaunt Dec 12 '10 at 17:19
  • 4
    @James if you want to see a language which maximizes the complexity of expressions, you should learn perl. – SoapBox Dec 12 '10 at 17:37
  • 1
    I honestly don't think compilers have any concept of cute or crazy... it's a GIGO world in there. – BoltClock Dec 12 '10 at 17:40
  • 4
    I think you're just used to using "cute" one instead of "crazy" one. `typedef` is not operator like `sizeof`, but seems to be treated like modifier, like `const` or `volatile`. – Pawel Zubrycki Dec 12 '10 at 17:45
  • @BoltClock : well that GIGO term sounds funny! – Nawaz Dec 12 '10 at 17:45
  • @Nawaz: http://www.catb.org/jargon/html/G/GIGO.html – BoltClock Dec 12 '10 at 17:51
  • @Nawaz: I'm not insulting you of course, sorry if it sounded like that. Just making fun of compilers and their "personalities" :P – BoltClock Dec 12 '10 at 18:02
  • @Bolt I always thought it was "get in, get out..." Huh. I probably have a lot of explaining to do... – Maxpm Jan 18 '11 at 22:11

4 Answers4

10

The question is "why it confuses you?"

The syntax comes from the grammar of declaration specifiers in C++, which is very general, and used for many things in C++. The order of declaration specifiers doesn't matter. Look at these:

int virtual f() const, *g(int);
int ptr1, *ptr2;
int typedef const *name1, name2;

These are equivalent to:

virtual int f() const;
virtual int* g(int);
int ptr1;
int *ptr2;
typedef const int* name1;
typedef const int name2;

If you look at them long enough you can find that the syntax is actually uniform and logical. Also reading the grammar of C++ may help.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • 2
    @ybungalobill : This post is good as far as knowing different syntax is concerned. but the question remains the same : why allow different syntaxes to achieve the very same thing? The real problem is, it confuses programmers as to which syntax is "really" different and really does different thing, and which syntax is different but does the very same thing. Programmers need to remember, say, 6 syntaxes which do ONLY 2 different things! – Nawaz Dec 12 '10 at 17:41
  • 3
    @Nawaz: The problem is that you consider these two forms as different syntaxes, but they actually the same. So I can argue that this way you don't need to remember the order of what comes first `inline`, `const`, `friend` or `int`. – Yakov Galka Dec 12 '10 at 17:44
  • @ybungalobill : if you're finding difficulty in understanding my comment, then please replace the term "syntaxes" with "forms". I mean, as long as there are more than one forms that do exactly the same thing, syntax is confusing, because there are other set of forms that do another thing. Just imagine, you've 100 forms that do one thing, and another 100 forms that do other one thing. These forms make unnecessarily difficult! – Nawaz Dec 12 '10 at 17:55
  • @Nawaz: I disagree. I don't know what your mother tongue is, but in e.g. Russian you can order the words in "you verb noun" in all the 6 possible ways. Yet it confuses no-one. I think it's applicable to computer languages too. – Yakov Galka Dec 12 '10 at 17:59
  • @Nawaz: Yet another argument: can you explain why you prefer one form to the other? How can you choose which one is more "natural"? – Yakov Galka Dec 12 '10 at 18:06
  • @ybungalobill : we're talking about C++, not human languages. And for your information, human languages have more crazy usage of words than computers do; human languages are sources of ambiguities (that is why C++ Standard uses specific grammar for the syntax to reduce ambiguities). A single sentence (in human language) is interpreted in many different ways! – Nawaz Dec 12 '10 at 18:08
  • @Nawaz : I'm not saying one is more natural than other. All I'm saying is if we have one from, then why the Standard allows other forms also? – Nawaz Dec 12 '10 at 18:09
  • @ybungalobill, @Nawaz: To avoid the decision of which one will be the standard one. Also this may make things simpler to somebody (**Including you!** read again my first comment!) – Yakov Galka Dec 12 '10 at 18:17
  • @ybugalobill : I'm not saying one is more natural than other. All I'm saying is if we have one from, then why the Standard allows other forms also? (Sorry the above comment was meant for you, but in hurry, I wrote my own name. LOL. Actually my mom was calling me for dinner.) – Nawaz Dec 12 '10 at 18:19
  • @ybugalobill : any of the various forms could be standard, while disallowing all others. And no, I don't think, allowing multiples forms/syntaxess make thing simpler. If it does, then I would want to know when, in what situations! – Nawaz Dec 12 '10 at 18:23
  • 3
    When I wrote with C#, it always annoyed me that it enforced order of "virtual" and "override" and others. – Johannes Schaub - litb Dec 13 '10 at 01:56
1

I was not aware of this syntax, though my g++ seems to accept it... but from a compiler standpoint, it makes parsing things harder: when you encounter an int token, you don't know if you're parsing a type definition (and a typedef is about to happen) or you're parsing a variable/function definition...

The only sense it makes to have A typedef B is if you consider typedef to be a binary operator (in the sense of a type assignment as A = B).

Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
0

I'm not sure that this is allowed by standart. But as we know there is a lot of things in c++ that allowed but have ilogical syntax. For instance this kind of things 1[a], "hello"[2];// ...

Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111