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

Not the parent poster either, but for me, it has been rvalue references/move semantics and variadic templates.

Rvalue references/move semantics: being able to explicitly express ownership with std::unique_ptr<T> and moving ownership with std::move() has made correct code so much easier to write (especially with unique_ptrs in containers). I don't ever use the delete operator anymore, yet my code leaks no memory and doesn't have the overhead of reference counting or garbage collection.

Variadic templates: these have made templates so much more useful. I've been able to write amazing helper functions that take an arbitrary number of arguments of any types, such as concat_string() [1], which is probably the most useful helper function I've ever written. I've also done some pretty far-out template meta-programming which I'm slightly embarrassed to talk about.

I was surprised by how useful move semantics were, unsurprised by variadic templates, but also surprised that lambdas weren't as significant as I had expected. They're very useful but haven't transformed the way I write code, perhaps because my C++ code was already pretty functional before C++11. Also lately I've been annoyed with lambdas because it's not possible to move an object into a closure, putting them in conflict with my beloved move semantics.

[1] http://www.agwa.name/projects/templates/



I've also found lambdas less life-changing than I'd hoped. In a lot of languages I use short single-expression closures all over the place, but C++11's lambda syntax is too verbose for that to work well. The difference between "o -> o.bar" (in C#) and "[](FooObject *o) { return o.bar; }" may seem somewhat superficial, but it greatly cuts down on how often the lambda is the natural and clean way to do something.

OTOH, having local functions with upvalues was a benefit of lambdas I hadn't even originally considered, but that I've found myself delighting in.


Yeah they are verbose (like so much in C++ sadly).

For frequently recurring patterns like "return o.bar" you can write a templated helper function that returns a functor which you can use like this:

  getmem(&FooObject::bar)
It's longer than the C# version but shorter than the lambda and easier to type because your editor can auto-complete most of it (unlike the lambda which has a lot of syntax). It becomes even shorter than the lambda if FooObject needs to be const.

I use this technique a lot. For instance I can do:

  std::sort(people.begin(), people.end(), compare_by_member(&Person::last_name))
to sort a container of Persons by last_name. I've been doing that since long before C++11 and still prefer it over lambdas because it's shorter, easier to type, and clearer when read.

Here's the code for compare_by_member if anyone's curious: https://gist.github.com/anonymous/5163229




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: