rvalue-reference

Do rvalue references to const have any use?

梦想与她 提交于 2019-12-17 02:33:55
问题 I guess not, but I would like to confirm. Is there any use for const Foo&& , where Foo is a class type? 回答1: They are occasionally useful. The draft C++0x itself uses them in a few places, for example: template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; The above two overloads ensure that the other ref(T&) and cref(const T&) functions do not bind to rvalues (which would otherwise be possible). Update I've just checked the official standard N3290,

Is returning by rvalue reference more efficient?

烂漫一生 提交于 2019-12-16 20:43:27
问题 for example: Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); } 回答1: Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); } This returns a dangling reference, just like with the lvalue reference case. After the function returns, the temporary object will get destructed. You should return Beta_ab by value, like the following Beta_ab Beta::toAB() const { return Beta_ab(1, 1); } Now, it's properly moving a temporary Beta_ab object into the return value of the function. If the

Is returning by rvalue reference more efficient?

丶灬走出姿态 提交于 2019-12-16 20:43:06
问题 for example: Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); } 回答1: Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); } This returns a dangling reference, just like with the lvalue reference case. After the function returns, the temporary object will get destructed. You should return Beta_ab by value, like the following Beta_ab Beta::toAB() const { return Beta_ab(1, 1); } Now, it's properly moving a temporary Beta_ab object into the return value of the function. If the

Disable temporary binding of Eigen expression to const references

别来无恙 提交于 2019-12-12 22:32:16
问题 I am trying to write a function that accepts only lvalue Eigen expressions passed via const references. My first idea was to keep only the overload const Eigen::MatrixBase<Derived>& and delete the Eigen::MatrixBase<Derived>&& one. To my surprise, the delete d function was not part of the overload candidate set. So I tried the code below #include <iostream> #include <Eigen/Dense> #define PRINT_MY_NAME std::cout << __PRETTY_FUNCTION__ << '\n' template<typename Derived> void f(const Eigen:

r-value causes a warning without the use of std::move

女生的网名这么多〃 提交于 2019-12-12 21:21:48
问题 Can someone help me to understand why the following code causes a warning struct A { A() : _a( 0 ) {} const int& _a; }; int main() { A a; } with warning warning: binding reference member '_a' to a temporary value [-Wdangling-field] A() : _a( 0 ) {} but this code, where std::move is used to initialize the member _a , does not: struct A { A() : _a( std::move(0) ) {} const int& _a; }; int main() { A a; } Aren't 0 and std::move( 0 ) both r-values? 回答1: This is an expression: 0 It's a very small

Eliminating unnecessary copies when building composite objects

前提是你 提交于 2019-12-12 18:30:03
问题 I was thinking of developing some named parameter code, but it got me thinking of some code like the following: #include <utility> int main() { using std::make_pair; auto x = make_pair(1, make_pair(2, make_pair(3, make_pair(4,5)))); } Now, a naive implementation of this would do "make_pair(4,5)" first, then copy the result into the second element of "make_pair(3, ...)", and then copy that into the second element of "make_pair(2, ...)" etc. This would result in O(n^2) performance unfortunately

c++11 emplace_back and push_back syntax with struct

旧时模样 提交于 2019-12-12 07:09:31
问题 I'm using MSVC, Visual Studio 2013. Suppose I have a struct: struct my_pair { int foo, bar; }; And I want to add a bunch of these efficiently, without too creating a temporary and then discarding it: vector<my_pair> v; v.push_back(41, 42); // does not work [a] v.push_back({41,42}); // works [b] v.emplace_back(41,42); // does not work [c] v.emplace_back({41,42}); // does not work [d] v.emplace_back(my_pair{41,42}); //works [e] Now if I add a constructor and copy constructor to my code: my_pair

Rvalue reference: Why aren't rvalues implicitly moved?

笑着哭i 提交于 2019-12-12 03:29:39
问题 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

Can't invoke or assign a std::function that has an rvalue reference as an argument (Visual C++)

女生的网名这么多〃 提交于 2019-12-11 19:19:25
问题 Seems like Visual C++'s std::function<> doesn't handle functions with rvalue refs as arguments. Can anyone suggest a workaround? #include <functional> using namespace std; class Object { }; void f(Object&&) { } auto g = [](Object&&){ }; function<void(Object&&)> h; int main() { Object o; f(move(o)); g(move(o)); // Uncomment any one of the following lines, and we get an error from the instantiation // of std::function: "error C2664: You cannot bind an lvalue to an rvalue reference" //h(move(o))

How to copy from primitive type variables when passing through rvalue reference function arguments

妖精的绣舞 提交于 2019-12-11 13:28:22
问题 I can copy from a non-primitive type variable by copy constructor and pass it through rvalue reference function argument. But how can do this with primitive type variables? for example: #include <cassert> #include <iostream> struct MyClass { int m = 0; }; MyClass& f(MyClass& x) { x.m++; return x; } inline MyClass f(MyClass&& x) { return f(x); } int& f(int& x) { x++; return x; } inline int f(int&& x) { return f(x); } int main() { MyClass x1; auto y1 = f(MyClass(x1)); // Calls f(MyClass&&) //