C++11 make_pair with specified template parameters doesn’t compile

This is not how std::make_pair is intended to be used; you are not supposed to explicitly specify the template arguments. The C++11 std::make_pair takes two arguments, of type T&& and U&&, where T and U are template type parameters. Effectively, it looks like this (ignoring the return type): template <typename T, typename U> [return type] … Read more

Move capture in lambda

Generalized lambda capture in C++14 In C++14 we will have the so called generalized lambda capture. This enables move capture. The following will be legal code in C++14: using namespace std; // a unique_ptr is move-only auto u = make_unique<some_type>( some, parameters ); // move the unique_ptr into the lambda go.run( [ u = move(u) … Read more

Is returning by rvalue reference more efficient?

Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); } This returns a dangling reference, just like with the lvalue reference case. After the function returns, the temporary object will get destructed. You should return Beta_ab by value, like the following Beta_ab Beta::toAB() const { return Beta_ab(1, 1); } Now, it’s properly moving a temporary Beta_ab object … Read more

C++11 rvalues and move semantics confusion (return statement)

First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); The first example returns a temporary which is caught by rval_ref. That temporary will have its life extended beyond the rval_ref definition and you can use it as if you had caught it by value. This is very similar to … Read more

What does T&& (double ampersand) mean in C++11?

It declares an rvalue reference (standards proposal doc). Here’s an introduction to rvalue references. Here’s a fantastic in-depth look at rvalue references by one of Microsoft’s standard library developers. CAUTION: the linked article on MSDN (“Rvalue References: C++0x Features in VC10, Part 2”) is a very clear introduction to Rvalue references, but makes statements about … Read more

Rule-of-Three becomes Rule-of-Five with C++11? [closed]

I’d say the Rule of Three becomes the Rule of Three, Four and Five: Each class should explicitly define exactly one of the following set of special member functions: None Destructor, copy constructor, copy assignment operator In addition, each class that explicitly defines a destructor may explicitly define a move constructor and/or a move assignment … Read more