5 ms·
I've been programming C++ for 20 years, and since C++11 the only feature I've seen that's worth the cognitive load is auto. Everything else just seems overly co
by mark-r 9y ago
I've been programming C++ for 20 years, and since C++11 the only feature I've seen that's worth the cognitive load is auto. Everything else just seems overly complicated. When I compare it to how easy things are in Python, I cry.
- umanwizard 9y agowhat about shared_ptr and unique_ptr ?
- mark-r 9y agoSorry, I misremembered the timeline. I thought they were part of C++03, but they're actually C++11. I was using shared_ptr when it was still part of Boost. I've edited my original comment to fix the error.
- umanwizard 9y agoBut `auto` was introduced in c++11, so your comment is still inaccurate ;)
- coldtea 9y agoParent said "since C++11 the only feature I've seen that's worth the cognitive load is auto". This means that (ignoring the mis-dating of the unique/shared_ptr) he already admitted, his comment is otherwise accurate.
- umanwizard 9y agoMy point was that `auto` was introduced in C++11, not "since C++11".
- coldtea 9y agoI don't think the parent's "since C++11" means "after C++11" but, "from C++11 and onwards". He explicitly said his reference was C++03
- snovv_crash 9y agoIterator loops? std::thread? std::unique_ptr? Move semantics? If these are causing hangups, you are overengineering your code.
- humanrebar 9y agoThe syntax might seem strange to you, but this is pretty slick: bool batch_failed = std::any_of( jobs.begin(), jobs.end(), [](const auto & job) { return job.failed(); } ); In C++03-plus-auto, you'd have to define an helper function or class: bool isJobFailed(const Job& job) { return job.failed(); } And then use find_if, I guess: auto last = jobs.end(); auto iter = std::find_if( jobs.begin(), jobs.end(), isJobFailed ); bool batch_failed = (iter == last);
- Koshkin 9y agoDoesn't look pretty, does it? Now compare this with what it could have been: jobs.Any(job => job.failed()) (Lambdas in C++ is one ridiculous example where I must use all the existing types of brackets in one expression: [](){} is a lambda.)
- nly 9y agoWhat is 'jobs' in your pseudo-code? Can it be a user-defined type? Does the author of that class need to explicitly state that their type meets some trait? Is 'Any' part of the type, a trait, or the language? What humanrebar wrote is an algorithm that will work with any range of any type that has a 'failed()' member function. Here's another formalisation: auto batch_failed = [](auto const& batch) { return std::any_of (begin (batch), end (batch), [](auto& testcase) { return testcase.failed (); }); } This function will basically work with anything.
- pcwalton 9y agoThis comment is nonresponsive to the parent comment. The complaint (which I happen to agree with) is that both iterators/ranges and lambda syntax are too verbose in C++.
- Davidbrcz 9y agoYou can't argue that the current C++ syntax is verbose and cumbersome. 99% of the time, I work on the full container, why not providing an overload which let me write at least bool batch_failed = std::any_of(jobs, [](const auto & job) { return job.failed();} ); Then, lambas are verbose. I would like to have a simpler syntax like for simple lambas (no capture, single expression in the lamba body). job could be automatically typed with const auto&, or you could write it yourself if you wish. job => return job.failed(); And a the current one, which is more verbose, for more complex lambas (capture, several expressions in the body)
- tempodox 9y agoAt least C++ lambdas are nice syntactic sugar for function objects, saving a good amount of boilerplate. When I look at lambdas in Python, I cry.
- kllrnohj 9y agostatic_assert, nullptr, constexpr, initialization lists, for-each loops, default & delete for class methods... and then there's things that are wonderful to use even if it's terrifying to look at how it's implemented like std::forward which is used with vector.emplace_back. there's also simple things like vector<unique_ptr<X>> being legal C++11 syntax instead of an illegal right shift operator.
- CyberDildonics 9y agoVariadic templates can be exceptionally useful in certain circumstances