5 ms·
C++23 drop the word auto? myLambda = [](&&x){ std::cout << x << '\n'; }; what's the point of having to write auto everywhere
by stabbles 5y ago
C++23 drop the word auto?
myLambda = [](&&x){ std::cout << x << '\n'; };
what's the point of having to write auto everywhere
- bruce343434 5y agosyntactical disambiguation, there's already something called "the most vexing parse". C++ is full of little holes like these and getting rid of auto would surely at least double compile times or at worst make parsing C++ actually impossible.
- gsliepen 5y agoYes, those things creep up in various places. Some are fixed, like when you have std::vector<std::vector<int>> where it used to give an error because it thought >> was an operator. But recently I discovered this one: int x[100] = {}; // just a regular array int val = x[[]{return 42;}()]; // compile error! Once again, greedy parsing is the culprit; here it thinks [[ is the start of an attribute.
- klyrs 5y agoOn one hand, nice catch... on the other hand... why do you want to do that??
- pjmlp 5y agoThat is what happens when a language makes the compromise to be copy-paste compatible with https://cdecl.org/ https://cdecl.org/
- ncmncm 5y agoMore to the point, it means the language continues to interpret header files from C libraries the same way that the C compiler always has.
- pjmlp 5y agoIt goes both ways, back in CFront days, and when it was picking up steam across MS-DOS, Mac OS, UNIX, among others. Unfortunely it is also an hindrance to fix some issues that prevent C++ to ever embrace safety, or better implementation of static analysers.
- ncmncm 5y agoBoth which ways? C was adopting C++ features before C90. Void type, function prototypes, scoped struct member tags. Declaration syntax is not an impediment to safety or to static analyzers, except insofar as it is trickier to code a parser for them.
- pjmlp 5y agoIt is in the sense that pointers everywhere cannot be prevented and it isn't always easy for a static analyser to actually make sense of how they are being used, as shown at some CppCon talks this year from Microsoft.
- ncmncm 5y agoDeclaration syntax has nothing to do with "pointers everywhere".
- steerablesafe 5y agoProblem is, function parameter names are optional, so you can already have something like `[](x){}`, where `x` is a type.
- kzrdude 5y agoAs seen from Python vs Lua: without a keyword for introduction it's less clear which scope a variable belongs to. Scopes are part of deterministic destruction in C++ and should not be taken lightly. As a general preference, I'd always want a keyword to introduce a new variable, and most languages seem to agree even if they didn't have to.
- gpderetta 5y agoIn this case x must obviously be in a new scope as all function parameters. There isn't possibly be an ambiguity here.
- rich_sasha 5y agoI think grandparent is referring to myLambda - that could be a new variable or overwriting of an existing one. I agree with them, in Python the number of times something like this happens: myVariable = 10 myVariablr = clever_function(myVariable) (note the typo in variable name)
- CraigJPerry 5y ago>> in Python the number of times something like this happens Anec-data but I’ve been writing python for 15+ years, I’ve contributed to various popular open source projects. I’ve never seen this in a code review. I’ve certainly never seen this kind of mistake released.
- NavinF 5y agoI've seen a variation of this bug several times in just the last year: myVariable = 1 if not something_unusual(): myVariablr = 2 return myVariablr This code will work just fine until something_unusual() returns True and then it crashes with UnboundLocalError: local variable 'myVariablr' referenced before assignment This really sucks when it happens during a long-running job. Say for example you're looping through an array in chunks and distributing each chunk across GPUs. The last chunk will have fewer elements than all the others, maybe even 1 element. If you don't pad the chunk, it will go through a different (and likely underutilized) code path downstream and crash like this.