10 ms·
C++26: A Placeholder with No Name
- SideburnsOfDoom 2y agoSee c#: "A discard communicates intent to the compiler and others that read your code: You intended to ignore it. You indicate that a variable is a discard by assigning it the underscore (_) as its name." https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals... 2020: http://dontcodetired.com/blog/post/Variables-We-Dont-Need-No-Stinking-Variables-C-Discards http://dontcodetired.com/blog/post/Variables-We-Dont-Need-No...
- tempodox 2y agoYep, Microsoft took only almost 50 years to do what Prolog could do in 1971, and the C++ standards committee took ~5 years longer.
- agumonkey 2y agoMakes me wonder when '_' was first used as a token to denote unused information. Prolog ? ML ?
- nicoburns 2y agoI think this predates computers entirely.
- SideburnsOfDoom 2y agoI think so, yes. English novels written in the 1800s would reference "Mr A------ from ------shire". As if they were redacting personally identifying information. (1) Underscores and dashes are not that different - especially as this is not just pre-computer but pre-typewriter. And now, underscores are a logical choice when the dash is already in use as a minus sign. 1) https://literature.stackexchange.com/questions/1944/why-are-place-names-obscured-in-charlotte-bront%C3%ABs-the-professor https://literature.stackexchange.com/questions/1944/why-are-... https://forums.welltrainedmind.com/topic/141704-jane-austens-mansfield-park-location-names-not-completed/ https://forums.welltrainedmind.com/topic/141704-jane-austens...
- adrian_b 2y agoUnderscore has been used in programming for the first time by the language IBM PL/I, in 1964-12, where it replaced the hyphen/minus character that was used by COBOL to make more readable the long identifiers. Replacing hyphen/minus with underscore has been done precisely for removing the ambiguity with the minus operator (In COBOL the ambiguity was less annoying, because the arithmetic operations were usually written with words, e.g. ADD, SUBTRACT, MULTIPLY and so on).
- agumonkey 2y agoGood point, this is clearly something that could go back centuries.. if not all the way back to clay tablets.
- lieks 2y agoIf we're counting only programming languages (and not fill-out forms), Prolog had it in 1971, before ML. ASCII didn't include _ until its 1963 draft, so it's probably somewhere in that time. I'll guess it's probably Prolog, but maybe Planner (Prolog's predecessor) had it too.
- fanf2 2y agoASCII 1963 was the first version of the standard and it lacked _ https://dl.acm.org/doi/10.1145/366707.367524 https://dl.acm.org/doi/10.1145/366707.367524 The 1965 draft had _ https://dl.acm.org/doi/10.1145/363831.363839 https://dl.acm.org/doi/10.1145/363831.363839 The first standard edition with _ was 1968 https://www.rfc-editor.org/info/rfc20 https://www.rfc-editor.org/info/rfc20 The 1977 version is also available https://nvlpubs.nist.gov/nistpubs/Legacy/FIPS/fipspub1-2-1977.pdf https://nvlpubs.nist.gov/nistpubs/Legacy/FIPS/fipspub1-2-197...
- deleted 2y ago[deleted]
- Kenji 2y ago[dead]
- kookamamie 2y agoWhat a convoluted mess.
- devnullbrain 2y agoI don't think the one character solution introduced at the bottom of TFA is convoluted. Could you explain why you think the solution - introduced at the bottom of TFA - is convoluted?
- asah 2y agoMy thought exactly. But that's C++, a bolted-on mess of crap whose redeeming features are (a) roughly the same execution performance as C and assembly, and (b) it's safer and higher productivity than C or assembly. At this point, it feels like a matter of time before Rust replaces C/C++.
- dgfitz 2y agoThere are decades and decades of code written in cobol that run the modern banking system. Multiply that code base size by like, 78.3, and you’re possibly in the same galaxy as the all the c++ codebases out there that will be maintained for the next 50 years. Rust may eat the lunch of c++ moving forward, the language will never go away.
- larodi 2y agoAbso-fkn-lutely! Stable computing protocols of all sorts don’t go away overnight and that is something everyone in IT should absolutely get used to. I expect C/C++ to live long beyond 2050.
- DowsingSpoon 2y ago>the language will never go away Just like COBOL! Seriously, _just like COBOL_. The language will fade in importance over time. C++ will be relegated to increasingly narrow niches. It will eventually only be found in "legacy" systems that no one wants to spend to rewrite. No one young will bother at all to learn C++ at all. Then, in a few decades, they'll be pulling folks like you and me out of retirement to maintain old systems for big bucks.
- wild_pointer 2y ago"In addition, there are some variables such as locks and scope_guards that are only used for their side effects" ... "This solution is also similar to other languages’ features or conventions" As far as I know, in Rust you can't use "_" for that, as the value will be dropped right away, so the mutex/resource/etc. won't live for the scope.
- awestroke 2y agoNo, it lives until the end of its last scope regardless of name
- tialaramex 2y agoThis is not a name, it's the specific choice not to assign it to any name, and so your parent was correct that it's dropped immediately. https://rust.godbolt.org/z/P1z7YeP4Y https://rust.godbolt.org/z/P1z7YeP4Y As you see, Rust specifically rejects this code because it's never what you meant, either write explicitly "I want to take and then immediately give away the lock" via drop(LOCK.lock()) (this really might be what you wanted, the Linux kernel does this in a few places) or write an actual named placeholder variable like in my third example function.
- SideburnsOfDoom 2y ago> This is not a name, it's the specific choice not to assign it to any name Yeah, it's the same in c#. This is noticeable when in the same scope you can have multlple "_" vars. If these were actual names, they would be a name clash. One of the uses is take some parts of a tuple return and ignore the others. e.g. var (thingIwant, _, _, _) = FunctionThaReturnsATupleOfFourThings(); There are three "_" in that expression, and they are different vars with different types. They don't really have the same name, they don't have names at all.
- vlovich123 2y agoBut on the other hand the motivating problems noted in C++ don't exist. It is 100% legal to completely rebind a variable in the same scope, it never warns that `_` variables are unused nor about unused `_` prefixed variables. I think `_` being immediately dropped is maybe one of those unfortunate decisions we'll look back in 20 years and regret.
- wiseowise 2y agoNot every "problem" needs a solution. At this point only LLMs will be able to decipher every intricacy of C++.
- devnullbrain 2y agoI swear some of you read 'C++' in the submission title and immediately assume it's tagging on inscrutable, additional ways of doing things. This is making C++ code _less_ intricate.
- lionkor 2y agoNo, they cannot, they are word prediction machines.
- tempodox 2y agoWhat a wise display of complete ignorance.
- ulbu 2y agohttps://www.london-fire.gov.uk/media/2045/london-frie-brigade-carers-hoarding_template_landing-page-panels.jpg https://www.london-fire.gov.uk/media/2045/london-frie-brigad... C++
- carom 2y agoI am so shocked at how many people use `auto` in C++. I can not think of a worse thing to do to your code in terms of readability and future maintainability. Maybe it is OK if you use an IDE to identify types for you but I still hate it. I am trying to learn a new library right now with light documentation which means reading the code, and between typedefs, auto, and technical debt, it is a tedious exercise to figure out something's type, to go look up its function, to see what type that is returning.
- kgeist 2y agoMy rule of thumb is to use auto only when the type is obvious from the context. I think it's a sane compromise between readability and non-verbosity.
- JTyQZSnP3cQGa8B 2y agoThe auto keyword should not go in a public API, but internally it's very useful especially when you create objects like `auto ob = make_unique<VeryLongClassName>(...);` or any other kind of function call where the type is obvious and would be identical on both sides of an assignment. As for your particular issue, using an IDE is essential, and the typedef keyword is almost obsolete, so I guess you stumbled upon a strange project. I would be curious to know what it is if it's open-source.
- carom 2y agoIt is Slang. A very cool project and it only got its public release relatively recently, so some sins are forgiven but there are so many typedefs. typedef struct ShaderReflection ProgramLayout; typedef enum SlangReflectionGenericArgType GenericArgType; https://github.com/shader-slang/slang/blob/master/include/slang.h https://github.com/shader-slang/slang/blob/master/include/sl...
- JTyQZSnP3cQGa8B 2y agoIt looks like C++98 to me: no `pragma once`, `typedef uint32_t SlangUInt32` seems strange, `typedef bool SlangBool` is definitely useless. The auto keyword is the least of my problems here. > years of collaboration between researchers at NVIDIA, Carnegie Mellon University, Stanford, MIT, UCSD and the University of Washington Now I understand why, it's the kind of project that you can't upgrade easily.
- emcell 2y agoevery time i see stuff like this, I hope I never have to work on c++ projects again.