rvalue-reference

Why does this function return an lvalue reference given rvalue arguments?

我的梦境 提交于 2019-11-29 17:26:25
问题 The following definition of a min function template <typename T, typename U> constexpr auto min(T&& t, U&& u) -> decltype(t < u ? t : u) { return t < u ? t : u; } has a problem: it seems that it's perfectly legal to write min(10, 20) = 0; This has been tested with Clang 3.5 and g++ 4.9. The solution is straightforward, just use std::forward to restore the "rvalue-ness" of the arguments, i.e. modify the body and the decltype to say t < u ? std::forward<T>(t) : std::forward<U>(u) However, I'm

Why do I need to use std::move in the initialization list of a move-constructor?

最后都变了- 提交于 2019-11-29 17:14:16
问题 Let's say I have a (trivial) class, which is move-constructible and move-assignable but not copy-constructable or copy-assignable: class movable { public: explicit movable(int) {} movable(movable&&) {} movable& operator=(movable&&) { return *this; } movable(const movable&) = delete; movable& operator=(const movable&) = delete; }; This works fine: movable m1(movable(17)); This, of course, does not work, because m1 is not an rvalue: movable m2(m1); But, I can wrap m1 in std::move , which casts

C++ operator overloading for pointers

拈花ヽ惹草 提交于 2019-11-29 14:39:28
I wonder (just out of curiosity) why operator overloading isn't allowed in C++ for pointers. I mean something like this: Vector2d* operator+(Vector2d* a, Vector2d* b) { return new Vector2d(a.x + b.x, a.y + b.y); } Vector2d* a = new Vector2d(1, 1); Vector2d* b = new Vector2d(2, 2); Vector2d* c = a + b; Note how 'a + b' creates a new Vector object, but then copies only its address into 'c', without calling a copy constructor. So it would sort of solve the same problem that the new rvalue references solve. Also, as far as I know, it's pretty much equivalent to what happens when using operator

Can someone explain rvalue references with respect to exceptions?

♀尐吖头ヾ 提交于 2019-11-29 14:36:35
问题 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 { throw std::exception("Oh no!"); // above is rvalue since it's got no name, what if the throw is made as // std::exception lvalExc("Oh wierd!"); // throw lvalExc; // if the throw is made thus, how can it be

How is it possible to get a reference to an rvalue?

大城市里の小女人 提交于 2019-11-29 13:59:27
问题 I have used std::move and std::forward in C++. My question is: how are these functions actually implemented by the standard library? If an lvalue is something you can get the address of, and an rvalue is exclusively not an lvalue, how can you actually implement these references? Do these new facilities allow for something like: auto x = &(3); or something like that? Can you get a reference to an rvalue that isn't just a std::move / forward returned lvalue? Hopefully these questions make sense

Standard library containers producing a lot of copies on rvalues in GCC

大兔子大兔子 提交于 2019-11-29 11:04:43
问题 I'm writing a app for both linux & windows, and noticed that the GCC build is producing a lot of useless calls to the copy constructor. Here's an example code to produce this behavior: struct A { A() { std::cout << "default" << std::endl; } A(A&& rvalue) { std::cout << "move" << std::endl; } A(const A& lvalue) { std::cout << "copy" << std::endl; } A& operator =(A a) { std::cout << "assign" << std::endl; return *this; } }; BOOST_AUTO_TEST_CASE(test_copy_semantics) { std::vector<A> vec_a( 3 );

Why doesn't C++ move construct rvalue references by default? [duplicate]

不羁的心 提交于 2019-11-29 09:29:33
This question already has an answer here: Rvalue Reference is Treated as an Lvalue? 4 answers Lvalue reference constructor is called instead of rvalue reference constructor 1 answer Say I have the following function void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } Why do I need to cast param back to an rvalue with std::move() ? Shouldn't it be obvious that the type of param is rvalue since it was declared in the function signature as an rvalue reference? Shouldn't the move constructor be automatically invoked here on this principle alone? Why

Return value or rvalue reference?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 09:28:37
In Scott Meyer's new book, he proposes an example usage for rvalue reference qualifiers that looks something like this: class Widget { private: DataType values; public: DataType& data() & { return values; } DataType data() && { return std::move(values); } // why DataType? }; So that: auto values = makeWidget().data(); move-constructs values instead of copy-constructing it. Why does the rvalue-ref-qualified data() return DataType instead of DataType&& ? auto would still deduce DataType in that case (although decltype(auto) wouldn't - but that can't be the only reason to prefer to return a value

C++0x: rvalue reference versus non-const lvalue

坚强是说给别人听的谎言 提交于 2019-11-29 07:22:57
问题 When programming in C++03, we can't pass an unnamed temporary T() to a function void foo(T&); . The usual solution is to give the temporary a name, and then pass it like: T v; foo(v); Now, along comes C++0x - and now with rvalue references, a function defined as void foo(T&&) will allow me to pass a temporary. Which brings me to my question: since a function that takes an rvalue reference can take both rvalue references (unnamed temporaries) as well as lvalue references (named non-const

c++11 optimal parameter passing

别等时光非礼了梦想. 提交于 2019-11-29 04:17:32
Consider these classes: #include <iostream> #include <string> class A { std::string test; public: A (std::string t) : test(std::move(t)) {} A (const A & other) { *this = other; } A (A && other) { *this = std::move(other); } A & operator = (const A & other) { std::cerr<<"copying A"<<std::endl; test = other.test; return *this; } A & operator = (A && other) { std::cerr<<"move A"<<std::endl; test = other.test; return *this; } }; class B { A a; public: B (A && a) : a(std::move(a)) {} B (A const & a) : a(a) {} }; When creating a B , I always have an optimal forward path for A , one move for rvalues