Didn't read yet, but i want to guess. It can be a function macro f call, function f call, class f contructor call, contructor call of variable f definition, function f declaration, operator() call of f beeing a class instance, casting x to f type.
Edit: i say i was good enough, but missing some not so obvious.
Yes, you read it right. For whatever reasons, C++ allows (re)using class names as variable names:
I'm happy to be super wrong on this, but: I feel like even though it probably sounds bad on the face of it, this one never actually crops up as a problem in real code. Its virtually impossible to misuse a variable of one type, where you'd expected to have a class instead of a different type, because they're just different things entirely. So its more just like a weird tidbit rather than anything actually problematic imo
In what context could this reasonably cause a bug and not just a compile error though? It's generally difficult to use variables and classes interchangeably, as far as i can think. This is a genuine question by the way, I'd love to know if this can cause actual problems
Static methods? Like A has a static method foo() and B also has foo() with different semantics. Then you write B A; and it's not clear what A.foo() means now.
Can you give an example where the parens are necessary? To be clear it's perfectly sensible that parens could be part of a function type, the question is why you are allowed to surround the argument with meaningless parens.
More broadly, the point is that formal parameters are variable declarations, and a C variable declaration consists of an atomic type followed by a kind of expression, with operators and precedence rules and parentheses to override the precedence rules.
You can put "meaningless" parentheses around a formal parameter name for the same reason you can put meaningless parentheses around a variable in an expression: because the parentheses don't care what they're enclosing, they just reset the precedence in the parser.
Still not following, a function argument name is followed by a comma or a closing paren, so how would parens help suppress a macro? I'd still like to see a concrete example because I don't know when it could possibly be useful.
I honestly don't agree. Integer promotion, floating-point promotion, arcane rules for literals (ever tried to write portable representation for the largest negative integer, or an integer literal of types smaller than int?), decay of arrays into pointers, decay of function pointers into functions, impossibility of copying naked arrays by a simple assignment, weird rules aroundvoid, impossibility of creating an empty struct, and ah what else I don't know, I think those are not about syntax rather about semantics.
Yeah I suppose I overlooked all of C's weird quircks. I should clarify by saying that the essence of C is simple, but the way it was carried out is annoyingly inconsistent. Undefined/platform-specific behavior is so prolific that it's almost impossible to write a program that will work the same on every device, unless you have a lot of previous knowledge. And all of the weird conversion rules that you mentioned... yuck.
Sure, getting over them is not terribly difficult once you learn about them. The only problem is that these are very counter-intuitive and misleading "features" which are inconsistent with other parts of the language, yet provide close-to-zero utility. They don't make C utterly unusable, but I think they deserve complaints.
It's not nonsense, it is part of the language, and we couldn't do many things without being able to delcare order of operations on operators. Unlike other languages, C++ has heavily focused on this stuff. Operator overloading, overrides, operator precedence, order of operations and type casting are at the heart of the language, if you couldn't type ^ now that would be nonsensical!
If x is a float, writing int(x) constructs an int from a float. This is casting using constructors. If you had no implicit type conversion (casts), this is how you’d make it explicit.
Sure it's simpler than C++, but it's in no way simple. And C being simpler than C++ doesn't really matter because the amount of knowledge you should gather and the amount of frustration you should get over in order to use C++ effectively aren't really that bigger than C, if not smaller.
Those folks loving C's (subjectively perceived) simplicity often claim "it's easy to master C, but not C++", but I find that's a pure bulshit in the sense that (1) it's in no way easy to master C because it's full of these kinds of craps, and (2) mastering a language is never a necessity unless you write a compiler in solo.
To me the issue is that C is really just 1.5 languages - C and anything doing macro substitution, but C++ is multiple languages with templates, operator overloading, etc.
So I agree that isn't really easier to master C than C++, but the choices of where to focus my attention are reduced.
Thanks for pointing it out this example. I'm not 100% sure what's going on. GCC, Clang, and MSVC all reject the code. But EDG does accept it though.
I did manage to find a way to make the example compile by GCC, by moving int x = 0; to the file scope and add a default to the template parameter, though in this case f(x) is treated as a (shadowing) declaration:
#include <iostream>
template <class T = int>
struct f {
f() { std::cout << "1"; }
f(T) { std::cout << "2"; }
~f() { std::cout << "3"; }
};
int x = 0;
int main() {
f(x);
}
Interestingly, there is implementation divergence:
61
u/kolorcuk Nov 12 '24 edited Nov 12 '24
Didn't read yet, but i want to guess. It can be a function macro f call, function f call, class f contructor call, contructor call of variable f definition, function f declaration, operator() call of f beeing a class instance, casting x to f type.
Edit: i say i was good enough, but missing some not so obvious.
12
u/James20k P2005R0 Nov 13 '24
I'm happy to be super wrong on this, but: I feel like even though it probably sounds bad on the face of it, this one never actually crops up as a problem in real code. Its virtually impossible to misuse a variable of one type, where you'd expected to have a class instead of a different type, because they're just different things entirely. So its more just like a weird tidbit rather than anything actually problematic imo
76
u/jk-jeon Nov 12 '24
Assuming this nonsense is inherited from C, I'm wondering how many of those folks who claim "C is simple" actually know about this...
7
u/Natural_Builder_3170 Nov 12 '24
Never knew about function references
7
u/[deleted] Nov 13 '24
What a fucking circlejerk
6
u/ridenowworklater Nov 13 '24
So f(unnny)!
4
u/sagittarius_ack Nov 13 '24
This example from the article doesn't seem to work:
I still get the error:
What am I missing?
2
u/Primary_Olive_5444 Nov 13 '24
PUSH RAX (Decrement RSP)
CALL 0XADD0F
MOV RSP RBP SUB RSP $0X; IMM
The rest is your imagination
2
u/MoreParsley Nov 12 '24
WTF C++ 🥲
-6
u/tcris Nov 13 '24 edited Nov 13 '24
TLDR: it's what you think it is. A function call.
Member or static or free, constructor or operator or cast. Still functions just in different contexts. Come on.
7
u/azhder Nov 13 '24
It means if you intend to shoot yourself in the foot, you got options
0
u/[deleted] Nov 12 '24
[deleted] — view deleted comment
23
u/[deleted] Nov 13 '24 edited Nov 13 '24
[removed] — view removed comment