rvalue-reference

Are moved-from objects required to be destructed?

谁都会走 提交于 2019-12-01 14:35:55
问题 If I move-construct a from b , is it still necessary to destruct b , or can I get away without doing so? This question crossed my mind during the implementation of an optional<T> template. Excerpt: ~optional() { if (initialized) { reinterpret_cast<T*>(data)->~T(); } } optional(optional&& o) : initialized(o.initialized) { if (initialized) { new(data) T(std::move(*o)); // move from o.data o.initialized = false; // o.data won't be destructed anymore! } } Of course, I could just replace the bool

C++11 templated function with rvalue param call

淺唱寂寞╮ 提交于 2019-12-01 14:12:15
In some class O, I have templated function test2 : struct A{int value;}; struct O{ A value; template<typename Args> static void test2(Args &&args){ std::cout << std::endl << "!!!" << std::is_rvalue_reference<decltype(args)>::value << std::endl; } }; Than, I want to call this function from another one: template<typename Args> void test(Args &&args){ using t = decltype(std::forward<Args>(args).value); std::cout << std::is_rvalue_reference<decltype(args)>::value; std::cout << std::is_rvalue_reference<decltype(std::forward<Args>(args).value)>::value; std::cout << std::is_rvalue_reference<t>::value

Why an Rvalue Reference is Turned into Lvalue Reference by a Universal Reference

喜夏-厌秋 提交于 2019-12-01 07:36:39
I suppose when a universal reference parameter is matched with an rvalue reference argument, an rvalue reference argument is returned. However, my testing shows that the rvalue reference is turned into a lvalue reference by the universal reference function template. Why is it so? #include <iostream> #include <type_traits> using namespace std; template <typename T> T f1(T&&t) { //<-----this is a universal reference cout << "is_lvalue reference:" << is_lvalue_reference<T>::value << endl; cout << "is_rvalue reference:" << is_rvalue_reference<T>::value << endl; cout << "is_reference:" << is

When will adding a move constructor and a move assignment operator really start make a difference?

淺唱寂寞╮ 提交于 2019-12-01 07:04:50
Considering the high quality of today's compilers regarding return value optimization (both RVO and NRVO), I was wondering at what class complexity it's actually meaningful to start adding move constructors and move assignment operators. For instance, for this really_trivial class, I just assume that move semantics cannot offer anything more than RVO and NRVO already does when copying around instances of it: class really_trivial { int first_; int second_; public: really_trivial(); ... }; While in this semi_complex class, I'd add a move constructor and move assignment operator without

Can't bind lvalue to rvalue reference

时光怂恿深爱的人放手 提交于 2019-12-01 04:47:45
I have this C++ test code snippet, #include <vector> class A { std::vector<int> x; public: A(std::vector<int>&& _x) : x(_x) {} }; class B { A a; public: B(std::vector<int>&& _x) : a(/*move(*/_x/*)*/) {} }; I'm passing _x to B as rvalue reference, but it's getting converted to lvalue when passed into A 's constructor and I have to use std::move() to make it work. My question is why _x is lvalue and not an rvalue reference in a() ? Quote from WIKI For safety reasons, some restrictions are imposed. A named variable will never be considered to be an rvalue even if it is declared as such. To get an

Is an xvalue's lifetime extended when it is bound to a const lvalue reference?

瘦欲@ 提交于 2019-12-01 03:50:18
If I write the following code: #include <iostream> using namespace std; int main() { cout << &(int &&)123 << endl; return 0; } Then g++ complains: foo.cc: In function ‘int main()’: foo.cc:7:20: error: taking address of xvalue (rvalue reference) Ok, thanks to What are rvalues, lvalues, xvalues, glvalues, and prvalues? I get that an xvalue means that it's about to "expire", which makes sense. But now if I do this: #include <iostream> using namespace std; int main() { const int &x = (int &&)123; cout << &x << endl; return 0; } This "works" just fine and will print an address. So, I have a few

Move from *this in an rvalue method?

最后都变了- 提交于 2019-12-01 03:40:50
In C++11, 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? } Unfortunately, I can't simply test this on my compiler since g++ does not support this feature yet :( The type of *this is always an lvalue: §9.3.2 [class.this] p1 In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose

Problem with “moveable-only types” in VC++ 2010

末鹿安然 提交于 2019-12-01 03:19:40
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_ptr is private. I tried inserting an lvalue by moving it, and it works just fine. #include <utility> #include <vector> int main() { typedef std::unique_ptr<int> int_ptr; int_ptr pi(new int(1)); std::vector<int_ptr> vec; vec.push_back(std::move(pi)); // OK vec

rvalue reference to function

Deadly 提交于 2019-12-01 00:56:47
问题 typedef void(&&RF)(void* p); RF rf() { return f; } int ay[10] = { 0 }; typedef int(&&RA)[10]; RA ra() { return ay; // error } cout << is_lvalue_reference<decltype(rf())>::value << endl; // 1 The C++ reference says "rvalue references to functions are treated as lvalues whether named or not". But I can not understand what the considerations for this are? I guess that perhaps the name of function is always a lvalue. So it must keep its attribute of an lvalue and ensure passing the function name

Problem with “moveable-only types” in VC++ 2010

天大地大妈咪最大 提交于 2019-11-30 23:30:44
问题 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_ptr is private. I tried inserting an lvalue by moving it, and it works just fine. #include <utility> #include <vector> int main() { typedef std::unique_ptr