rvalue-reference

Is a data member of a temporary object an xvalue in C++11?

别来无恙 提交于 2019-11-30 22:37:16
问题 #include <vector> using namespace std; struct A { vector<int> coll; }; void f(const vector<int>&){} void f(vector<int>&&){} int main() { f(A().coll); // Is "A().coll" an xvalue? } Does C++11 guarantee f(A().coll) will call void f(vector<int>&&) ? 回答1: Yes. C++14 standard, §5.2.5/4.2, given E1.E2 : If E2 is a non-static data member and the type of E1 is “ cq1 vq1 X ”, and the type of E2 is “ cq2 vq2 T ”, the expression designates the named member of the object designated by the first

Visual C++ 2010, rvalue reference bug?

女生的网名这么多〃 提交于 2019-11-30 22:16:52
Is it a bug in Visual C++ 2010 or right behaviour? template<class T> T f(T const &r) { return r; } template<class T> T f(T &&r) { static_assert(false, "no way"); //< line # 10 return r; } int main() { int y = 4; f(y); //< line # 17 } I thought, the function f(T &&) should never be called but it's called with T = int &. The output: main.cpp(10): error C2338: no way main.cpp(17) : see reference to function template instantiation 'T f(T)' being compiled with [ T=int & ] Update 1 Do you know any C++x0 compiler as a reference? I've tried comeau online test-drive but could not compile r-value

Are objects inside rvalue referenced object, also rvalue referenced?

我怕爱的太早我们不能终老 提交于 2019-11-30 21:31:37
Are objects inside rvalue referenced object, also rvalue referenced? struct A{ }; struct B{ A a2; }; //template<class B> void test(B &&b){ // 1. Is this the correct way? auto &&in3 = std::forward<B>(b).a2; std::cout << std::is_rvalue_reference<decltype(in3)>::value; // return true // 2. or this? auto &&in4 = b.a2; std::cout << std::is_rvalue_reference<decltype(in4)>::value; // return false } test(B()); http://coliru.stacked-crooked.com/a/bcf0f7dc4cc0440e Yes, members of rvalues are themselves rvalues. This was clarified by DR 421 But that is irrelevant here: auto &&in4 = b.a2; b is not an

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

走远了吗. 提交于 2019-11-30 17:39:32
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'm not 100% sure, so please explain your answer. There is no move construction going on. std::move()

Are objects inside rvalue referenced object, also rvalue referenced?

丶灬走出姿态 提交于 2019-11-30 17:15:38
问题 Are objects inside rvalue referenced object, also rvalue referenced? struct A{ }; struct B{ A a2; }; //template<class B> void test(B &&b){ // 1. Is this the correct way? auto &&in3 = std::forward<B>(b).a2; std::cout << std::is_rvalue_reference<decltype(in3)>::value; // return true // 2. or this? auto &&in4 = b.a2; std::cout << std::is_rvalue_reference<decltype(in4)>::value; // return false } test(B()); http://coliru.stacked-crooked.com/a/bcf0f7dc4cc0440e 回答1: Yes, members of rvalues are

Lifetime extension and the conditional operator

隐身守侯 提交于 2019-11-30 13:42:33
问题 local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string& a = std::string("hello"); std::string&& b = std::string("world"); Does that also work when the initializer is not a simple expression, but uses the conditional operator? std::string&& c = condition ? std::string("hello") : std::string("world"); What if one of the results is a temporary object, but the other one isn't? std::string d = "hello"; const std::string& e = condition ? d :

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

本小妞迷上赌 提交于 2019-11-30 11:46:53
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 it to an rvalue-reference, to make it work: movable m2(std::move(m1)); So far, so good. Now, let's say

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

醉酒当歌 提交于 2019-11-30 11:42:29
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 at a loss to explain why the first definition doesn't generate an error. Given my understanding of

C++11: Why is assigning rvalues allowed?

送分小仙女□ 提交于 2019-11-30 10:02:16
From what I understand the reason why it is dangerous to return rvalues references to from functions is due to the following code: T&& f(T&& x) { do_something_to_T(x); return static_cast<T&&>(x); } T f(const T& x) { T x2 = x; do_something_to_T(x2); return x2; } T&& y = f(T()); This leaves y as an undefined dangling reference. However, I don't understand why the above code even compiles? Is there ever a legitimate reason to assign a rvalue reference to another rvalue reference? Aren't rvalues suppose to be, roughly speaking, "temporaries", i.e. going to be made invalid at the end of the

Ambiguous call with overloaded r-value reference function

蓝咒 提交于 2019-11-30 09:56:59
问题 I have a class with the following declarations: class IcoSphere { [...] private: int _addVertex(const glm::vec3 &p); int addVertex(glm::vec3 p); int addVertex(const glm::vec3 &&p); [...] }; Then, I'm calling 'addVertex' like so: IcoSphere sphere; double t = (1.0 +sqrt(5.0)) /2.0; sphere.addVertex(glm::vec3(-1,t,0)); The argument for 'addVertex' is obviously not a reference, and yet the g++-compiler throws the following error: ./network/icosphere.cpp: In static member function ‘static void