> Even if you don’t need to handle several types, this can be useful to avoid repetition and make the code more compact and readable. ... > namespace1::namespace2::namespace3::ACertainTypeOfWidget Deeply nested namespaces are problematic in themselves due to namespace resolution (e.g. see https://abseil.io/tips/130 ), but templates should never be an answer to "my type is too long to type out". You're hurting yoursel…
> templates should never be an answer to "my type is too long to type out" Eh, in some cases it's a wash. std::vector > objects; // ... auto it = std::find_if(objects.begin(), objects.end(), [](auto& widget) { return widget->x == 1; }); Sure, you could repeat the type `std::unique_ptr ` in the lambda, but that's just noise. You don't spell out the types like that either in, say, C#. Yes, compilation time is an epsilo…
The Evolutions of Lambdas in C++14, C++17 and C++20
61–70 of 75 posts
Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#62Regarding returning lambdas, of course it could be done even in c++11 via std::function.
Problems with std::function:- 1. It can not hold move only callable objects. 2. It heap allocate stored callable object if the object is large enough.
Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#63Earlier quoted context omitted.
syntactical 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.
Yes, those things creep up in various places. Some are fixed, like when you have std::vector > 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.
Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#64Earlier quoted context omitted.
More to the point, it means the language continues to interpret header files from C libraries the same way that the C compiler always has.
It 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.
Declaration syntax is not an impediment to safety or to static analyzers, except insofar as it is trickier to code a parser for them.
Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#65Earlier quoted context omitted.
Yes, deeply nested namespaces are not nice. I think boost really dropped the ball on this. boost::asio::ip::tcp should have been boost::tcp. Are they trying to be as bad as java?
As the article explains it's not "as bad as Java" it's worse, what Java does actually makes sense in Java, maybe you have to type slightly more characters but Java is already a verbose language best suited to heavy tool-assist. However it doesn't make sense in C++ because the benefits Java gets don't apply and the price is heavier. There are a bunch of things like this in C++ where superficially C++ feature X is like…
Absl's arguments are that you shouldn't use that structure much, so you should avoid deep nesting (specifically deep nesting, not no nesting). But it actually does something in C++, which isn't true in Java. And that's really the core of Absl's complaints about nesting - don't copy your Java package name (which is irrelevant useless noise) to your C++ code (where it's actual structure, and collisions can result in issues)
Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#66Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#67Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#68Earlier quoted context omitted.
As the article explains it's not "as bad as Java" it's worse, what Java does actually makes sense in Java, maybe you have to type slightly more characters but Java is already a verbose language best suited to heavy tool-assist. However it doesn't make sense in C++ because the benefits Java gets don't apply and the price is heavier. There are a bunch of things like this in C++ where superficially C++ feature X is like…
Your argument seems to be "Java can be bad at this because it assumes an IDE, but C++ can't because it can't assume an IDE". Which is nonsense. If anything nested namespaces make sense in C++ but don't in Java, since Java has no nesting semantics and doesn't have using aliases. In Java it's just raw noise, whereas in C++ it's actual structure. Java isn't getting "benefits" from deep package paths. Absl's arguments ar…
If you introduce ambiguity, the code won't compile.
In C++ too bad, you wrote Doodad and thought you were getting ::some::new::package::hierarchy::Doodad however that doesn't actually exist, you were thinking of ::some::long::package::hierarchy::Doodad and your compiler didn't complain because it silently concludes you wanted ::some::Doodad which does exist (maybe it was added years ago and is rarely used, or maybe it was added in the new version you got last week) even though you never actually wanted anything from ::some itself. C++ loves ambiguity, because now it's your fault this happened, you were less than perfect and as such unworthy of the power and grace of C++
Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#69> Even if you don’t need to handle several types, this can be useful to avoid repetition and make the code more compact and readable. ... > namespace1::namespace2::namespace3::ACertainTypeOfWidget Deeply nested namespaces are problematic in themselves due to namespace resolution (e.g. see https://abseil.io/tips/130 ), but templates should never be an answer to "my type is too long to type out". You're hurting yoursel…
> templates should never be an answer to "my type is too long to type out" Eh, in some cases it's a wash. std::vector > objects; // ... auto it = std::find_if(objects.begin(), objects.end(), [](auto& widget) { return widget->x == 1; }); Sure, you could repeat the type `std::unique_ptr ` in the lambda, but that's just noise. You don't spell out the types like that either in, say, C#. Yes, compilation time is an epsilo…
Yes, it's noise to write:
std::unique_ptr widget = std::make_unique();
But I don't think it's so noisy in the case you've provided. If I encountered that code, I'd wonder whether that operator-> is safe, or if you're inadvertently dropping some qualifier (e.g. is it actually a raw pointer?).And as I suggested, it's not much harder to add a using decl:
using UniqueWidget = std::unique_ptr;
// ...
std::vector objects;
// ...
auto it = std::find_if(objects.begin(), objects.end(), [](UniqueWidget&) { return widget->x == 1; });
(I also recognize that my opinion is very heavily flavored by the Google style guide: https://google.github.io/styleguide/cppguide.html#Type_deduc...)Re: The Evolutions of Lambdas in C++14, C++17 and C++20
#70Earlier quoted context omitted.
It 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.
Both 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.