rvalue-reference

C++11 binding rules for const &&

我与影子孤独终老i 提交于 2019-12-21 17:34:50
问题 Many people do not know that const rvalue references are part of the C++11 language. This blog post discusses them but appears to be mistaken regarding the binding rules. Quoting the blog: struct s {}; void f ( s&); // #1 void f (const s&); // #2 void f ( s&&); // #3 void f (const s&&); // #4 const s g (); s x; const s cx; f (s ()); // rvalue #3, #4, #2 f (g ()); // const rvalue #4, #2 f (x); // lvalue #1, #2 f (cx); // const lvalue #2 Note the asymmetry: while a const lvalue reference can

Why do we need to set rvalue reference to null in move constructor?

僤鯓⒐⒋嵵緔 提交于 2019-12-21 07:55:58
问题 //code from https://skillsmatter.com/skillscasts/2188-move-semanticsperfect-forwarding-and-rvalue-references class Widget { public: Widget(Widget&& rhs) : pds(rhs.pds) // take source’s value { rhs.pds = nullptr; // why?? } private: struct DataStructure; DataStructure *pds; }; I can't understand the reason for setting rhd.pds to nullptr . What will happen if we remove this line : rhs.pds = nullptr; 回答1: Some details of the class have been removed. In particular, the constructor dynamically

Under what conditions should I be thinking about implementing a move constructor and a move operator?

血红的双手。 提交于 2019-12-21 04:36:14
问题 For standard copy constructors and assignment operators, I always think about implementing them or delete ing the defaults out of existence, if my class implements a destructor. For the new move constructor and move operator , what is the right way to think about whether or not an implementation is necessary? As a first pass of transitioning a system from pre-C++0x, could I just delete the default move constructor and move operator or should I leave them alone? 回答1: You don't have to worry

Explicit ref-qualified conversion operator templates in action

五迷三道 提交于 2019-12-21 04:27:17
问题 Given the following conversion operators struct A { template<typename T> explicit operator T&& () &&; template<typename T> explicit operator T& () &; template<typename T> explicit operator const T& () const&; }; struct B {}; I would expect the following conversions to be all valid, yet some give compile errors (live example): A a; A&& ar = std::move(a); A& al = a; const A& ac = a; B&& bm(std::move(a)); // 1. OK B&& bt(A{}); // 2. OK B&& br(ar); // 3. error: no viable conversion from A to B B&

Move semantics - what it's all about? [duplicate]

筅森魡賤 提交于 2019-12-20 12:38:08
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Can someone please explain move semantics to me? Could someone point me to a good source or explain it here what are the move semantics? 回答1: Forget about C++0x for the moment. Move semantics are something that is language independent -- C++0x merely provides a standard way to perform operations with move semantics. Definition Move semantics define the behaviour of certain operations. Most of the time they are

Correct use of `= delete` for methods in classes

﹥>﹥吖頭↗ 提交于 2019-12-20 08:48:55
问题 Is the following snipplet correct for un-defining all otherwise generated methods and constructors for a class? struct Picture { // 'explicit': no accidental cast from string to Picture explicit Picture(const string &filename) { /* load image from file */ } // no accidental construction, i.e. temporaries and the like Picture() = delete; // no copy Picture(const Picture&) = delete; // no assign Picture& operator=(const Picture&) = delete; // no move Picture(Picture&&) = delete; // no move

Forbid rvalue binding via constructor to member const reference

我与影子孤独终老i 提交于 2019-12-20 03:24:11
问题 I am working on a matrix view class, of which constructor takes a matrix as a parameter and binds it to a const reference member. I would very much like to avoid binding rvalues, since they don't bind via a constructor parameter, and we end up with a dangling reference. I came up with the following (simplified code): struct Foo{}; class X { const Foo& _foo; public: X(const Foo&&) = delete; // prevents rvalue binding X(const Foo& foo): _foo(foo){} // lvalue is OK }; Foo get_Foo() { return {};

Apparently missing overload of getline() taking RRef to stream in GCC 4.7.2 and Clang 3.2

拟墨画扇 提交于 2019-12-20 01:33:34
问题 I ran into an unexpected compilation error when trying to use getline() with a temporary stream object: #include <iostream> #include <string> #include <sstream> using namespace std; int main() { string input = "hello\nworld\nof\ndelimiters"; string line; if (getline(stringstream(input), line)) // ERROR! { cout << line << endl; } } It looks like no overload of getline() exists that accepts an rvalue reference to a stream object. If I change main() to use an lvalue, it compiles and runs as

Does `const &&` bind to all prvalues (and xvalues)?

坚强是说给别人听的谎言 提交于 2019-12-19 17:34:10
问题 The C++ standard defines the following functions deleted; template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; This is to aid in ensuring that the functions are not misused by disallowing them from binding to temporary values (rvalues). Does const && bind to all rvalues, specifically prvalues? Would const && bind to all "moved objects" (xvalues; basically something returned from std::move or similar)? I can reason that it should, but I don't have

Does `const &&` bind to all prvalues (and xvalues)?

做~自己de王妃 提交于 2019-12-19 17:32:21
问题 The C++ standard defines the following functions deleted; template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; This is to aid in ensuring that the functions are not misused by disallowing them from binding to temporary values (rvalues). Does const && bind to all rvalues, specifically prvalues? Would const && bind to all "moved objects" (xvalues; basically something returned from std::move or similar)? I can reason that it should, but I don't have