rvalue-reference

When is the move constructor called in the `std::move()` function?

允我心安 提交于 2019-12-18 19:12:21
问题 The function std::move() is defined as template<typename T> typename std::remove_reference<T>::type&& move(T && t) { return static_cast<typename std::remove_reference<T>::type&&>( t ); } There are four places where I can imagine the move constructor to be called: When the parameter is passed. When the cast is performed. When the result is returned. Not in the std::move() function itself but possibly at the place where the returned reference ultimately arrives. I would bet for number 4, but I

When is the move constructor called in the `std::move()` function?

ⅰ亾dé卋堺 提交于 2019-12-18 19:12:07
问题 The function std::move() is defined as template<typename T> typename std::remove_reference<T>::type&& move(T && t) { return static_cast<typename std::remove_reference<T>::type&&>( t ); } There are four places where I can imagine the move constructor to be called: When the parameter is passed. When the cast is performed. When the result is returned. Not in the std::move() function itself but possibly at the place where the returned reference ultimately arrives. I would bet for number 4, but I

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

被刻印的时光 ゝ 提交于 2019-12-18 12:54:39
问题 This is a follow-on question to C++0x 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 should probably be called because of the implicit temporary, and this happens in GCC but not MSVC (or the EDG front-end used in MSVC's Intellisense). What about this code? void f(std::string &&); //NB: No

c++11 optimal parameter passing

折月煮酒 提交于 2019-12-18 04:14:53
问题 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 &

C++11 rvalue reference calling copy constructor too

断了今生、忘了曾经 提交于 2019-12-17 22:58:37
问题 I've been testing some C++11 features from some some. I came across r-value references and move constructors. I implemented my first move constructor, here it is: #include <iostream> #include <vector> using namespace std; class TestClass{ public: TestClass(int s): size(s), arr(new int[s]){ } ~TestClass(){ if (arr) delete arr; } // copy constructor TestClass(const TestClass& other): size(other.size), arr(new int[other.size]){ std::copy(other.arr, other.arr + other.size, arr); } // move

Why use identity in forward definition for C++0x rvalue reference?

我是研究僧i 提交于 2019-12-17 22:46:57
问题 In A Brief Introduction to Rvalue References, forward is defined as follows: template <typename T> struct identity { typedef T type; }; template <typename T> T &&forward(typename identity<T>::type &&a) { return a; } What purpose does the identity class perform? Why not: template <typename T> T &&forward(T &&a) { return a; } 回答1: The purpose of identity was to make T non-deducible. That is, to force the client to explicitly supply T when calling forward . forward(a); // compile-time error

visual studio implementation of “move semantics” and “rvalue reference”

余生长醉 提交于 2019-12-17 19:46:52
问题 I came across a Youtube video on c++11 concurrency (part 3) and the following code, which compiles and generates correct result in the video. However, I got a compile error of this code using Visual Studio 2012. The compiler complains about the argument type of toSin(list<double>&&) . If I change the argument type to list<double>& , the code compiled. My question is what is returned from move(list) in the _tmain() , is it a rvalue reference or just a reference? #include "stdafx.h" #include

construction helper make_XYZ allowing RVO and type deduction even if XZY has noncopy constraint

萝らか妹 提交于 2019-12-17 18:29:40
问题 UPDATE1: C++17 added type deduction for constructors - which does not imply that the free function is an inferior solution. UPDATE2: C++17 added guaranteed copy elision (the copy does not even take place conceptually). So with C++17 my code actually works and with optimal performance. But Martinho's code using brace initialisation for the return value is still the cleaner solution I believe. But checkout this answer from Barry and the comment from T.C. OLD POST: Type deduction does not work

Passing by value vs const & and && overloads

扶醉桌前 提交于 2019-12-17 17:43:08
问题 So after looking up move semantics I see that general consensus is to pass by value when you intend to transfer ownership. But in Scott Meyer's talk on Universal references I've noticed that std::vector::push_back has 2 overloads: void push_back( const T& value ); void push_back( T&& value ); So I thought to myself, wouldn't void push_back( T value ); be enough? I've asked a few people which ultimately lead to the following test case: #include <memory> #include <iostream> #include <type

Do C++11 compilers turn local variables into rvalues when they can during code optimization?

给你一囗甜甜゛ 提交于 2019-12-17 16:03:09
问题 Sometimes it's wise to split complicated or long expressions into multiple steps, for example (the 2nd version isn't more clear, but it's just an example): return object1(object2(object3(x))); can be written as: object3 a(x); object2 b(a); object1 c(b); return c; Assuming all 3 classes implement constructors that take rvalue as a parameter, the first version might be faster, because temporary objects are passed and can be moved. I'm assuming that in the 2nd version, the local variables are