C/C++ Declaration Syntax
The cluster discusses the confusing and ambiguous variable declaration syntax in C and C++, such as 'declaration follows usage,' pointer notation, and parsing issues like the most vexing parse, often comparing it to clearer alternatives in languages like Rust, Go, and others.
Activity Over Time
Top Contributors
Keywords
Sample Comments
what's stopping a new version of C from defining a better declaration syntax? (better = easier to read, write and understand)
It seems like you have the concept down pat. I don't see what's wrong with it?Like lots of languages use positionally-dependent symbols. In both Perl and VB various characters in the variable name implied things about its type. With C/C++ the position of * tells you lots of important things. How is this anything less than yet another random language quirk?
The reason some languages use "let name: type" instead of "type name" is to eliminate various ambiguities, for example C++'s most vexing parse, or the classic C typedef ambiguity where a statement like "X * Y" is ambiguous because it could be the declaration of a variable Y whose type is "X*" where "X" is a typedef, or it could be the multiplication of a variable "X" with a variable "Y". There are many languages where the
A great example of "declaration follows use" outside of C syntax.
Wait - are we talking C/C++ here? where a function call, a variable declaration, an expression cast all look identical?
Pointer semantics. The syntax in general bugs me. It's too close to C++ for my taste -- too many braces and brackets and sigils.
I find your comment inflammatory and your zealousness amusing, but I'll bite and try to be reasonable by assuming you're willing to entertain another point of view.The whole "declaration follows usage" is just a bad tradeoff. It makes it easier to parse expressions. That's my understanding of why they did it. It makes it _objectively_ harder to read, because some declarations follow this easy pattern of "name on the right and type on the left", while for som
This is syntax sugar. Donβt conflate syntactic niceties with the semantic absurdity that C++ has.
I never use comma, I always define one variable per line. Much easier to edit and read. However, the way it works is certainly not inconsistent.As to your other concerns, it makes perfect sense. int WHATEVER; read as "WHATEVER is an int" and you can work backwards from there. There is no more type syntax than that. The thing is, it's much easier to look at stuff this way since otherwise you'll have to shift back and forth between 2 different s
For someone who uses Reddit and HN too much I sure can forget about markdown.The function syntax C++ has seems more like a bandaid around the broken scoping rules inherited by C more than anything. If you were writing a replacement it seems like it would be one of your first considerations to make each statement scope limited to the scope of the declaration. I mean, that is what auto is effectively doing in C++ nowadays - you have to evaluate the whole statement to resolve what auto is suppos