rvalue-reference

What is R-Value reference that is about to come in next c++ standard?

What is R-Value reference that is about to come in next c++ standard? ...

Is returning by rvalue reference more efficient?

for example: Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); } ...

Are return values going to be passed by rvalue reference in c++0x?

Let's say I have a function: typedef std::vector<int> VecType; VecType randomVector(); int processing() { VecType v = randomVector(); return std::accumulate(v.begin(), v.end(), 0); } Does C++0x specifically say the spurious copy will be averted from the return value of randomVector? Or would a compiler need to implement the R...

C++0x "move from" container

In C++0x, we get an efficiency boost concerning containers with std::move: SomeExpensiveType x = /* ... */; vec.push_back(std::move(x)); But I can't find anything going the other way. What I mean is something like this: SomeExpensiveType x = vec.back(); // copy! vec.pop_back(); // argh This is more frequent (the copy-pop) on adapte...

non-class rvalues always have cv-unqualified types

§3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&& i) { std::cout << "const rvalue\n"; } int main() { pass_int(foo()); // prints "rvalu...

Some clarification on rvalue references

First: where are std::move and std::forward defined? I know what they do, but I can't find proof that any standard header is required to include them. In gcc44 sometimes std::move is available, and sometimes its not, so a definitive include directive would be useful. When implementing move semantics, the source is presumably left in a...

Problem with "moveable-only types" in VC++ 2010

I recently installed Visual Studio 2010 Professional RC to try it out and test the few C++0x features that are implemented in VC++ 2010. I instantiated a std::vector of std::unique_ptr, without any problems. However, when I try to populate it by passing temporaries to push_back, the compiler complains that the copy constructor of unique...

How can I get this code involving unique_ptr to compile?!

#include <vector> #include <memory> using namespace std; class A { public: A(): i(new int) {} A(A const& a) = delete; A(A &&a): i(move(a.i)) {} unique_ptr<int> i; }; class AGroup { public: void AddA(A &&a) { a_.emplace_back(move(a)); } vector<A> a_; }; int main() { AGroup ag; ag.Ad...

How to reduce redundant code when adding new c++0x rvalue reference operator overloads

I am adding new operator overloads to take advantage of c++0x rvalue references, and I feel like I'm producing a lot of redundant code. I have a class, tree, that holds a tree of algebraic operations on double values. Here is an example use case: tree x = 1.23; tree y = 8.19; tree z = (x + y)/67.31 - 3.15*y; ... std::cout << z; // prin...

C++0x rvalue references and temporaries

(I asked a variation of this question on comp.std.c++ but didn't get an answer.) Why does the call to f(arg) in this code call the const ref overload of f? void f(const std::string &); //less efficient void f(std::string &&); //more efficient void g(const char * arg) { f(arg); } My intuition says that the f(string &&) overload ...

C++0x rvalue references - lvalues-rvalue binding

This is a follow-on question to http://stackoverflow.com/questions/2748866/c0x-rvalue-references-and-temporaries In the previous question, I asked how this code should work: void f(const std::string &); //less efficient void f(std::string &&); //more efficient void g(const char * arg) { f(arg); } It seems that the move overload...

Overload on reference, versus sole pass-by-value + std::move?

It seems the main advice concerning C++0x's rvalues is to add move constructors and move operators to your classes, until compilers default-implement them. But waiting is a losing strategy if you use VC10, because automatic generation probably won't be here until VC10 SP1, or in worst case, VC11. Likely, the wait for this will be measur...

Move from *this in an rvalue method?

In C++0x, methods can be overloaded on whether or not the expression that denotes the object on which the method is called is an lvalue or an rvalue. If I return *this from a method called via an rvalue, do I need to explicitly move from *this or not? Foo Foo::method() && { return std::move(*this); // Is this move required or not?...

min and perfect forwarding

The min algorithm is normally expressed like this: template <typename T> const T& min(const T& x, const T& y) { return y < x ? y : x; } However, this does not allow constructs of the form min(a, b) = 0. You can achieve that with an additional overload: template <typename T> T& min(T& x, T& y) { return y < x ? y : x; } What ...

Do rvalue references allow implicit conversions?

Is the following code legal? std::string&& x = "hello world"; g++ 4.5.0 compiles this code without any problems. ...

Taking the Address of a Temporary, with a Twist

I have a function address_of, which returns a Pointer (encapsulating a shared_ptr) to its argument. address_of needs to work on both lvalues and rvalues, so there are two versions of address_of: one accepting a reference and the other accepting an rvalue reference. Since taking the address of a temporary is a Bad Thing, the rvalue versio...

Problem by a reference variable of a template parameter

The following small example shows my problem: template<class T> struct X { static void xxx(T& x) { } static void xxx(T&& x) { } }; int main(int argc, char** argv) { int x = 9; X<int>::xxx(x); // OK. X<int&>::xxx(x); // ERROR! return 0; } Error message (GCC): error: ‘static void X::xxx(T&&) [with T = int&]’...

Is it bad form to provide only a move constructor?

I would like to return a noncopyable object of type Foo from a function. This is basically a helper object which the caller will use to perform a set of actions, with a destructor to perform some cleanup after the actions are complete. Before the advent of rvalue references, I would have returned a shared_ptr<Foo> or something similar. ...

Move constructor (rvalue reference) in implicit conversion

Hi all, I am upgrading a C++ project from MSVC 2008 to 2010, and because of the new CComBSTR move constructor [CComBSTR( CComBSTR&& )], I am getting a compiler error because of an ambiguous call. Essentially, we have a String class, very similar to std::wstring that have a cast operator to CComBSTR. This is similator to the following ...

Can someone explain rvalue references with respect to exceptions?

Lets say I've this exception class: struct MyException : public std::exception { MyException(const std::exception &exc) : std::exception(exc) { cout << "lval\n"; } MyException(std::exception &&exc) : std::exception(std::forward<std::exception>(exc)) { cout << "rval\n"; } }; ... ... try { thr...