rvalue-reference

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

≡放荡痞女 提交于 2019-11-28 20:28:06
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; } 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 forward<A>(a); // ok The reason this is necessary is because the template parameter is the switch with which

Lvalue to rvalue reference binding

China☆狼群 提交于 2019-11-28 19:32:13
The compiler keeps complaining I'm trying to bind an lvalue to an rvalue reference, but I cannot see how. I'm new to C++11, move semantics, etc., so please bear with me. I have this function: template <typename Key, typename Value, typename HashFunction, typename Equals> Value& FastHash<Key, Value, HashFunction, Equals>::operator[](Key&& key) { // Some code here... Insert(key, Value()); // Compiler error here // More code here. } which calls this method: template <typename Key, typename Value, typename HashFunction, typename Equals> void FastHash<Key, Value, HashFunction, Equals>::Insert(Key&&

What are “rvalue references for *this” for?

对着背影说爱祢 提交于 2019-11-28 18:46:01
What are the most typical use cases of "rvalue references for *this" which the standard also calls reference qualifiers for member functions? By the way, there is a really good explanation about this language feature here . Andrew Tomazos When called, each member function has an implicit object parameter that *this references. So (a) these normal function overloads: void f(const T&); void f(T&&); when called like f(x) ; and (b) these member function overloads: struct C { void f() const &; void f() &&; }; when called like x.f() - both (a) and (b) dispatch with similar viability and ranking. So

How would one call std::forward on all arguments in a variadic function?

Deadly 提交于 2019-11-28 15:56:14
I was just writing a generic object factory and using the boost preprocessor meta-library to make a variadic template (using 2010 and it doesn't support them). My function uses rval references and std::forward to do perfect forwarding and it got me thinking...when C++0X comes out and I had a standard compiler I would do this with real variadic templates. How though, would I call std::forward on the arguments? template <typename ...Params> void f(Params... params) // how do I say these are rvalue reference? { y(std::forward(...params)); //? - I doubt this would work. } Only way I can think of

Can I reuse an rvalue reference parameter to return an rvalue reference?

ぃ、小莉子 提交于 2019-11-28 12:14:40
Consider the following code: struct MyString { // some ctors MyString& operator+=( const MyString& other ); // implemented correctly }; MyString operator+( const MyString& lhs, const MyString& rhs ) { MyString nrv( lhs ); nrv += rhs; return nrv; } MyString&& operator+( MyString&& lhs, const MyString& rhs ) { lhs += rhs; return std::move( lhs ); // return the rvalue reference we received as a parameter! } This works for the following use-case MyString a, b, c; // initialized properly MyString result = a + b + c; But it creates a dangling reference for const MyString& result = a + b + c; Now, I

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

自作多情 提交于 2019-11-28 11:30:57
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 <iostream> #include <thread> #include <chrono> #include <list> #include <algorithm> using namespace std;

Rvalue reference: Why aren't rvalues implicitly moved?

霸气de小男生 提交于 2019-11-28 11:11:06
On Artima article about C++ rvalue reference ( http://www.artima.com/cppsource/rvalue.html ) there is words: That's why it is necessary to say move(x) instead of just x when passing down to the base class. This is a key safety feature of move semantics designed to prevent accidently moving twice from some named variable. I can't think situation when such double move can perform. Can you give an example of this? In other words, what will go wrong if all members of T&& would be rvalue references and not just references? Consider this scenario: void foo(std::string x) {} void bar(std::string y) {

C++ operator overloading for pointers

霸气de小男生 提交于 2019-11-28 08:57:25
问题 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

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

╄→尐↘猪︶ㄣ 提交于 2019-11-28 07:38:59
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 for constructors (at least until and including C++11). The common solution is to rely on RVO (Return

Perfect forwarding a member of object

五迷三道 提交于 2019-11-28 07:36:59
Suppose I have two struct s: struct X {}; struct Y { X x; } I have functions: void f(X&); void f(X&&); How do I write a function g() that takes Y& or Y&& but perfect forwarding X& or X&& to f() , respectively: template <typename T> void g(T&& t) { if (is_lvalue_reference<T>::value) { f(t.x); } else { f(move(t.x)); } } The above code illustrate my intention but is not very scalable as the number of parameters grows. Is there a way make it work for perfect forwarding and make it scalable? template <typename T> void g(T&& t) { f(std::forward<T>(t).x); } I think this will work, although I'm not