4 ms·
In C++ it's an idiom to use immediately invoked lambdas: auto x = []{ /*...*/ return 5; }(); There is/was an attempt to introduce a more of a firs
by badmintonbaseba 2y ago
In C++ it's an idiom to use immediately invoked lambdas:
auto x = []{
/*...*/
return 5;
}();
There is/was an attempt to introduce a more of a first-class language construct for such immediate "block expressions":
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2806r2.html https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p28...
I'm not convinced that automatic checking of copy-paste errors of such blocks make much sense though. At least I think the false positive rate would be way too high.
- wruza 2y agoIIFE exist, but are cumbersome to type/read in most languages. C++ is probably the winner by syntax and semantics here. false positive rate would be way too high The key idea is not to have identical blocks, but to have a way to overview changes in similar code, similar by origin and design. It’s a snippet diff tool, not a typo autocorrector. There’s no false positives cause if “common foo” has zero diff in all cases, it probably should be “foo(…)”.
- sltkr 2y agoGCC has supported statement expressions for ages: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html They're also used extensively in the Linux kernel, mostly to implement macros: https://github.com/search?q=repo%3Atorvalds%2Flinux%20%22(%7B%22&type=code https://github.com/search?q=repo%3Atorvalds%2Flinux%20%22(%7...
- mkoubaa 2y agoNow if only c++ could guarantee copy elision from lambda returns...
- badmintonbaseba 2y agoThere is a lot of guaranteed copy elision since C++17, what exactly do you mean?
- pjmlp 2y agoIt depends if using C++17 and later versions.
- deleted 2y ago[deleted]