rvalue-reference

std::thread with movable, non-copyable argument

≡放荡痞女 提交于 2019-12-05 19:44:06
问题 The following program doesn't build in VS11 beta, gcc 4.5, or clang 3.1 #include <thread> #include <memory> int main() { std::unique_ptr<int> p; std::thread th([](std::unique_ptr<int>) { },std::move(p)); th.join(); } This is because the argument type is not copyable, but the implementation attempts to copy it. As far as I can tell, this program is well formed and should work. The requirements for std::thread seem to imply that movable, non-copyable arguments should work here. Specifically it

Is it necessary to define move constructors from different classes?

大兔子大兔子 提交于 2019-12-05 19:11:23
问题 Consider the following: struct X { Y y_; X(const Y & y) :y_(y) {} X(Y && y) :y_(std::move(y)) {} }; Is it necessary to define a constructor like the second one in order to take full advantage of move semantics? Or will it be taken care of automatically in the appropriate situations? 回答1: Yes, but no. Your code should just be this: struct X { Y y_; X(Y y) : // either copy, move, or elide a Y y_(std::move(y)) // and move it to the member {} }; If you ever say in design "I need my own copy of

Lvalue reference constructor is called instead of rvalue reference constructor

大憨熊 提交于 2019-12-05 10:41:41
There is this code: #include <iostream> class F { public: F() = default; F(F&&) { std::cout << "F(F&&)" << std::endl; } F(F&) { std::cout << "F(F&)" << std::endl; } }; class G { F f_; public: G(F&& f) : f_(f) { std::cout << "G()" << std::endl; } }; int main(){ G g = F(); return 0; } The output is: F(F&) G() Why F(F&) constructor is called instead of F(F&&) constructor in constructor of class G ? The parameter for constructor of class G is F&& f which is rvalue reference but constructor for lvalue reference is called. Why F(F&) constructor is called instead of F(F&&) constructor in constructor

Perfect Forwarding Variadic Template to Standard Thread

强颜欢笑 提交于 2019-12-05 09:54:19
I'm trying to make a form of std::thread that puts a wrapper around the code executed in the thread. Unfortunately I can't get it to compile due likely to my poor understanding of rvalues and the Function templated type I'm trying to pass. Here's my code: #include <vector> #include <thread> #include <utility> void Simple2(int a, int b) {} template <typename Function, typename... Args> void Wrapper(Function&& f, Args&&... a) { f(std::forward<Args>(a)...); } class Pool { public: template <typename Function, typename... Args> void Binder(Function&& f, Args&&... a) { std::thread t(Wrapper<Function

Which type to declare to avoid copy and make sure returned value is moved

a 夏天 提交于 2019-12-05 08:00:58
问题 Suppose that I have a function A f() ; I want to initialize a local variable a to the return value of f ; I don't want to rely on RVO; What is the best option (and why) to avoid the return value of f being copied when a may have to be modified I know that a will not be modified Options: a) A a = f(); b) A&& a = f(); c) const A& = f(); d) const A&& = f(); EDIT: I would say: b) c) Because both use references and avoid an extra copy (which could be avoided by RVO as well, but that is not

Is there any case where a return of a RValue Reference (&&) is useful?

谁说胖子不能爱 提交于 2019-12-05 07:54:59
Is there a reason when a function should return a RValue Reference ? A technique, or trick, or an idiom or pattern? MyClass&& func( ... ); I am aware of the danger of returning references in general, but sometimes we do it anyway, don't we ( T& T::operator=(T) is just one idiomatic example). But how about T&& func(...) ? Is there any general place where we would gain from doing that? Probably different when one writes library or API code, compared to just client code? There are a few occasions when it is appropriate, but they are relatively rare. The case comes up in one example when you want

move constructor and std::move confusion

ⅰ亾dé卋堺 提交于 2019-12-05 06:22:33
I am reading about the std::move , move constructor and move assignment operator. To be honest, all I got now is confusion. Now I have a class: class A{ public: int key; int value; A(){key = 3; value = 4;} //Simple move constructor A(A&& B){ A.key = std::move(B.key); A.value = std::move(B.value);} }; I thought B is an rvalue reference, why you can apply std::move to an ravlue reference's member? After B.key and B.value have been moved, both have been invalidated, but how B as an object of class A gets invalidated? What if I have A a(A()) , A() is apparently an rvlaue, can A() be moved by std:

How are rvalues in c++ stored in memory?

孤者浪人 提交于 2019-12-05 05:19:27
Trying to learn lvalues , rvalues and memory allocation for them. So with a lot of learning materials there is a bit of chaos. An rvalue is a value that needs to exist only in bounds of a expression where it was created (until C++11 at least). So it has an address and block of memory that it occupies. But by definition we cannot get an address of rvalue , because it is a temporary object in contrast to an lvalue . But even before C++11 we were able to get an address of rvalue by returning it from a function and saving it into a const reference type (uh, I guess not an address but a value). So,

C++11 constructor argument: std::move and value or std::forward and rvalue reference

社会主义新天地 提交于 2019-12-05 04:00:39
问题 Which of the below two should be preferred and why? struct X { Y data_; explicit X(Y&& data): data_(std::forward<Y>(data)) {} }; vs struct X { Y data_; explicit X(Y data): data_(std::move(data)) {} }; 回答1: The two variants differ in functionality. The following statements work for the second one–but not for the first one: Y y; X x(y); If you are looking for the same functionality, the two variants should look as follows: struct X { Y data_; explicit X(const Y& data) : data_(data) { } explicit

Temporary const array not binding to rvalue reference

元气小坏坏 提交于 2019-12-05 01:59:06
I have the following test program: #include <iostream> #include <type_traits> #include <utility> template<typename Ty, std::size_t N> void foo(Ty (&&)[N]) { std::cout << "Ty (&&)[" << N << "]\t" << std::is_const<Ty>::value << '\n'; } template<typename Ty, std::size_t N> void foo(Ty (&)[N]) { std::cout << "Ty (&)[" << N << "]\t" << std::is_const<Ty>::value << '\n'; } template<typename Ty> using id = Ty; int main() { std::cout.setf(std::cout.boolalpha); foo(id<int[]>{1, 2, 3, 4, 5}); foo(id<int const[]>{1, 2, 3, 4, 5}); // <-- HERE. int xs[]{1, 2, 3, 4, 5}; foo(xs); int const ys[]{1, 2, 3, 4, 5}