Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Iterator loops? std::thread? std::unique_ptr? Move semantics?

If these are causing hangups, you are overengineering your code.


At 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.


static_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.


The 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);


Doesn'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.)


What 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.


You 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)


This 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++.


what about shared_ptr and unique_ptr ?


Sorry, 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.


But `auto` was introduced in c++11, so your comment is still inaccurate ;)


Parent 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.


My point was that `auto` was introduced in C++11, not "since C++11".


I 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


Variadic templates can be exceptionally useful in certain circumstances




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: