rvalue-reference

Why do some people use swap for move assignments?

只愿长相守 提交于 2019-11-26 08:48:54
问题 For example, stdlibc++ has the following: unique_lock& operator=(unique_lock&& __u) { if(_M_owns) unlock(); unique_lock(std::move(__u)).swap(*this); __u._M_device = 0; __u._M_owns = false; return *this; } Why not just assign the two __u members to *this directly? Doesn\'t the swap imply that __u is assigned the *this members, only to later have then assigned 0 and false... in which case the swap is doing unnecessary work. What am I missing? (the unique_lock::swap just does an std::swap on

Why are rvalues references variables not rvalue?

不羁的心 提交于 2019-11-26 08:32:15
问题 Let\'s say I have two overloads of a function f . f(T&&) and f(T&) . Then in the body of g : g(T&& t) { f(t);} the overload f(T&) will be called because t is considered an lvalue. This is very surprising to me. How a function with signature f(T&&) can not match a call with type T&& ? What suprises me even more is that a call f(static_cast<T&&>(t)) would actually call the rvalue overload f(T&&) . What are the C++ rules that make this possible? Is T&& more than a type? 回答1: The things that are

In C++, what categories (lvalue, rvalue, xvalue, etc.) can expressions that produce temporaries of class type fall into?

南笙酒味 提交于 2019-11-26 06:41:47
问题 Here is some example code: #include <iostream> class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { return (void*)this; } friend Foo operator + (const Foo& lhs, const Foo& rhs); friend std::ostream& operator << (std::ostream& os, const Foo& f); private: int data; }; std::ostream& operator << (std::ostream& os, const Foo& f) { return (os << f.data); } Foo operator + (const Foo& lhs, const Foo& rhs) { return Foo(lhs.data + rhs

Why user-defined move-constructor disables the implicit copy-constructor?

与世无争的帅哥 提交于 2019-11-26 06:30:58
问题 While I\'m reading boost/shared_ptr.hpp, i saw this code: // generated copy constructor, destructor are fine... #if defined( BOOST_HAS_RVALUE_REFS ) // ... except in C++0x, move disables the implicit copy shared_ptr( shared_ptr const & r ): px( r.px ), pn( r.pn ) // never throws { } #endif What does the comment \"generated copy constructor, destructor are fine except in C++11, move disables the implicit copy\" mean here? Shall we always write the copy ctor ourselves to prevent this situation

C++11 make_pair with specified template parameters doesn&#39;t compile

喜欢而已 提交于 2019-11-26 03:53:51
I was just playing around with g++ 4.7 (one of the later snapshots) with -std=c++11 enabled. I tried to compile some of my existing code base and one case that failed somewhat confuses me. I would appreciate if someone can explain what is going on. Here's the code: #include <utility> #include <iostream> #include <vector> #include <string> int main ( ) { std::string s = "abc"; // 1 ok std::pair < std::string, int > a = std::make_pair ( s, 7 ); // 2 error on the next line std::pair < std::string, int > b = std::make_pair < std::string, int > ( s, 7 ); // 3 ok std::pair < std::string, int > d =

Move capture in lambda

强颜欢笑 提交于 2019-11-26 03:24:14
问题 How do I capture by move (also known as rvalue reference) in a C++11 lambda? I am trying to write something like this: std::unique_ptr<int> myPointer(new int); std::function<void(void)> example = [std::move(myPointer)]{ *myPointer = 4; }; 回答1: Generalized lambda capture in C++14 In C++14 we will have the so called generalized lambda capture. This enables move capture. The following will be legal code in C++14: using namespace std; // a unique_ptr is move-only auto u = make_unique<some_type>(

Why is `std::move` named `std::move`?

丶灬走出姿态 提交于 2019-11-26 02:40:20
问题 The C++11 std::move(x) function doesn\'t really move anything at all. It is just a cast to r-value. Why was this done? Isn\'t this misleading? 回答1: It is correct that std::move(x) is just a cast to rvalue - more specifically to an xvalue, as opposed to a prvalue. And it is also true that having a cast named move sometimes confuses people. However the intent of this naming is not to confuse, but rather to make your code more readable. The history of move dates back to the original move

C++11 make_pair with specified template parameters doesn&#39;t compile

荒凉一梦 提交于 2019-11-26 01:53:15
问题 I was just playing around with g++ 4.7 (one of the later snapshots) with -std=c++11 enabled. I tried to compile some of my existing code base and one case that failed somewhat confuses me. I would appreciate if someone can explain what is going on. Here\'s the code: #include <utility> #include <iostream> #include <vector> #include <string> int main ( ) { std::string s = \"abc\"; // 1 ok std::pair < std::string, int > a = std::make_pair ( s, 7 ); // 2 error on the next line std::pair < std:

Move capture in lambda

痞子三分冷 提交于 2019-11-26 00:51:45
How do I capture by move (also known as rvalue reference) in a C++11 lambda? I am trying to write something like this: std::unique_ptr<int> myPointer(new int); std::function<void(void)> example = [std::move(myPointer)]{ *myPointer = 4; }; Ralph Tandetzky Generalized lambda capture in C++14 In C++14 we will have the so called generalized lambda capture . This enables move capture. The following will be legal code in C++14: using namespace std; // a unique_ptr is move-only auto u = make_unique<some_type>( some, parameters ); // move the unique_ptr into the lambda go.run( [ u{move(u)} ] { do

Rvalue Reference is Treated as an Lvalue?

有些话、适合烂在心里 提交于 2019-11-26 00:29:21
问题 I posted this answer: https://stackoverflow.com/a/28459180/2642059 Which contains the following code: void foo(string&& bar){ string* temp = &bar; cout << *temp << \" @:\" << temp << endl; } Is bar an rvalue or an lvalue? I ask because I obviously cannot take the address of an rvalue, yet I can take the address of an rvalue reference as is done here. If you can perform any operation on an rvalue reference that you can on an lvalue reference what is the point in differentiating between the two