167 points · by ingve · 4y ago · 73 comments
...
> 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 yourself at compile-time, and your code is now more error-prone as you can pass in any argument.
Just add a using-decl (or a namespace alias if the base name isn't meaningful enough for you).
Eh, in some cases it's a wash.
std::vector<std::unique_ptr<mynamespace::MyWidget>> 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<mynamespace::MyWidget>` 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 epsilon slower, but that consideration loses to readability any time of day. (Otherwise you could just remove all comments and indentation from your code - that also makes compilation faster!)Yes, it's noise to write:
std::unique_ptr<MyWidget> widget = std::make_unique<MyWidget>();
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<mynamespace::MyWidget>;
// ...
std::vector<UniqueWidget> 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...)There are a bunch of things like this in C++ where superficially C++ feature X is like feature Y in another language, and so C++ programmers wrongly assume the problems with feature X must also plague feature Y. I'm sure the reverse happens too.
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)
Yes! I've noticed this also. Several recent C++ changes are copying idioms from other languages, and sometimes the standards committee seems to have missed the point or misunderstood how the features are used in the language they're copying.
The results are superficially similar, but not quite what I'd expect after knowing the feature from the source language.
I wouldn't say it's a smaller evolution than previous iterations.
myLambda = [](&&x){ std::cout << x << '\n'; };
what's the point of having to write auto everywhereAs 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.
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) 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. Generalised capture
In C++11, lambdas can only capture existing objects in their scope:
int z = 42;
auto myLambda = [z](int x){ std::cout << x << '-' << z + 2 << '\n'; };
...
But with the powerful generalised lambda capture, we can initialise captured values with about anything.
int z = 42;
auto myLambda = [y = z + 2](int x){ std::cout << x << '-' << y << '\n'; };
Am I the only one who doesn't see much of a difference here? std::function<int()> make_func(std::unique_ptr<int> ptr) {
return [p = std::move(ptr)]() { return *p; }
}
Without generalized capture syntax, there's no good way to transfer ownership into the closure (a regular `[ptr]` capture would attempt to make a copy, and a by-ref `[&ptr]` capture would lead to a use-after-free).(Or maybe you're talking about putting a lambda into an std::function, which is a totally different thing and will probably incur heap allocation?)
Lambda expressions do not involve any kind of hidden allocations, their definition is precisely formalized and can be reviewed in S 7.5.5 of the standard. Even if you don't care to read the standard, there is no shortage of resources online that explain what a lambda expression is, and none of them involve anything to do with hidden allocations:
https://en.cppreference.com/w/cpp/language/lambda
I'm sorry to pick on you specifically, but it's a major problem that is entirely unnecessary. It's almost heartbreaking that the people who should have the most experience in a subject based on decades of knowledge are often the ones who carry the biggest misconceptions and spread the most misinformation.
I have no time to read the standard or keep up with it. Maybe that is what people at Google can afford to do? I'm guessing that is where you work.
I run a small business, and I have to juggle a lot more than just writing code. When I have a problem, I figure out where it's coming from, and fix it. If you run your own business I'm sure you understand that.
Not keeping up with the standard has nothing to do with ignorance or refusal, but with constraints. I also don't think it should be required to know the standard, or the syntax for features you don't need to use everyday of the top of your head. If you are not sure, look it up. That's what the internet is for.
In any case, no need to be arrogant and dismiss people with experience as stupid or ignorant, or blaming them for spreading misinformation, simply because they don't know certain syntax by heart or don't keep up with the standard.
This is an unsubstantiated point and simply untrue. Modern features have worked hard to maintain what is referred to as "zero cost abstractions".
>If you run your own business I'm sure you understand that.
I do own and operate a business, a quant firm engaged in HFT. It's part of the reason why I hire so many C++ developers.
>Not keeping up with the standard has nothing to do with ignorance or refusal, but with constraints.
The ignorance comes from going into an interview where one would presumably be expected to have a basic understand of modern C++ functionality and then proceeding to call that interview BS when in fact it was you who simply had a poor understanding of what was expected.
If you don't want to use lambda expressions or you feel like using C++ like it's still the year 2003, so be it, that is your choice... just don't be surprised and go around calling interviewers full of BS when you continuously refuse to keep your understanding of technology up to date.
The problem in this case is not with the "BS interview" or the interviewer, the problem is with you. If you do not keep your understanding of C++ up to date, for whatever reason, then an interviewer is right to reject you for it in favor of someone who does take the time to keep their skills up-to-date.
Internally, lambdas are function pointers + the context, and the context may or may not be dynamically allocated depending on how lambdas are used.
Lambdas are essentially syntactic sugar for an anonymous struct holding references to/copies of captured variables and an operator() method with your code. Not 100% precise, but memory-wise that's how it works (and has to work). No dynamic memory allocation is involved here at all.
If you put a lambda (or any other callable) in a std::function, the std::function will copy that functor object, either into its own small-buffer-optimized in-place storage or (above a certain functor size) into heap-allocated memory.
1. It can not hold move only callable objects.
2. It heap allocate stored callable object if the object is large enough.
4. Can't be constexpr
Example: https://github.com/facebook/folly/blob/main/folly/docs/Funct...
I am a big friend to push good coding practices into the CI/CD configuration, regardless of the programming language.
Anyone can do whatever they feel like on their own computer, but the overall product has guidelines to follow, specially relevant in projects that live from changing consulting companies all the time.