6 ms·
The auto keyword seems like a strange addition given it's already a C keyword with a different meaning and this change won't help developers that much. It's mor
by z_open 3y ago
The auto keyword seems like a strange addition given it's already a C keyword with a different meaning and this change won't help developers that much. It's more useful in C++.
- masklinn 3y agoIt's more useful in C++ with things like generic and voldemort types, but I still would not discount it in C as the lack of namespacing impacts type names, and it could limit convenience typedefs (those which exist only to avoid the `struct` and `enum` prefixes). It might also help transitioning to fixed integer types, which are rather verbose.
- kevin_thibedeau 3y agoC has generics as well. This permits the return type of generic functions to be preserved.
- jcranmer 3y agoThe only generic feature C has is the _Generic expression, which isn't a function, but closer to a switch(typeof(x)) expression.
- kevin_thibedeau 3y ago_Generic can be used to merge a collection of functions.
- CyberDildonics 3y agoThat's not the same as being able to write a data structure that takes any type.
- kevin_thibedeau 3y agoIt's still generic. You don't have to have feature parity with C++ to meet that bar.
- CyberDildonics 3y agoThat would be like calling function overloading generics.
- neutrono 3y agoIt should have been called _Overload or something similar, since it's not really a generic.
- rightbyte 3y agoHah. Voldemort types. The ones that can't be named (in practice becouse too long to fit on a line or remember)?
- gpderetta 3y agoAlso because they might be a) an undocumented implementation detail (the result of std::bind for example); b) utterly unutterable like the type of a lambda expression.
- masklinn 3y ago> The ones that can't be named (in practice becouse too long to fit on a line or remember)? Nah those are easy to name, just annoying, it's types which literally don't have an external / public name, like lambdas, or locally defined types.
- bheadmaster 3y agoIt's actually a struct that only has a name in the scope of the function which returns `auto`, and thus cannot be named outside of it. Like this: #include <iostream> auto createVoldemortType(int value) { struct Voldemort { int value; }; return Voldemort{value}; } int main() { auto voldemort = createVoldemortType(7); std::cout << voldemort.value << std::endl; // output: 7 }
- krater23 3y agoThis way to code is a good reason to make auto the same tabu as goto.
- trealira 3y agoI wonder how much this complicates parsing C++. Because of this, you can't discard/free struct and class definitions as soon as you leave the scope, like you can in C, because the definition can still escape the scope by being returned from a function with the "auto" keyword.
- 3y ago
- anthomtb 3y agoAre there any publicly-viewable C codebases that make use of pre-C23 auto? When I learned C (2003-ish) auto vs. register was at best a footnote that came up when discussing static. I agree that you don't need auto pointers in C the way you do in C++. C++ type names can get so cumbersome...much easier to let the compiler figure it out for you.
- andoaaaaz662 3y ago[dead]
- trealira 3y agoIt could help in generic macros, like this: #define SWAP(var1, var2) do { \ auto tmp = var1; \ var1 = var2; \ var2 = tmp; \ } while (0) Previously, you would have needed a third macro for the type, or you would have needed to do a byteswap to be generic.
- unwind 3y agoTrue, and now we also get typeof() so there is more than one way! const typeof(var1) tmp = var1;
- kstrauser 3y agoWhy’s that inside do/while?
- asplake 3y agoTo scope the temporaries?
- peterfirefly 3y agoNo, a block would be enough for that. It's to make ; handling natural when the macro is used.
- glouwbug 3y agoForces the use of a semicolon: SWAP(…);
- trealira 3y agoIf you simply put braces around it, you'd generate syntactically invalid code when you don't use braces for if-else statements. Consider this: if (e) SWAP(a, b); else something_else(); Currently, that expands to this, which is still valid code: if (e) do { // ...macro... } while(0); else something_else(); If it were just wrapped in braces, the code would be parsed like this: // One-armed if-statement if (e) { // ...macro... } // Empty statement ; // Another statement else something_else(); It's incorrect syntax to start a statement with else, so the compiler will say something like "Unexpected 'else' at line N."
- andoaaaaz662 3y agoDefinitely my least favorite of the new additions. The rest of them are good but I have a feeling auto and typeof on the left hand of an assignment are both going to be considered no-nos in real codebases. I think they obfuscate things unnecessarily. In C++ it's understandable because with templates you have a tendency to have massively complicated type names. Everything else looks great to me though.
- Conscat 3y agoIt standardizes existing common practice. Everyone already has `__auto_type` macros, but nearly nobody was using `auto` as a synonym for `int`.