rvalue-reference

Why unique_ptr and shared_ptr do not invalidate the pointer they are constructed from?

一曲冷凌霜 提交于 2020-03-15 05:46:42
问题 A note: this is an API design question , riding on the design of the constructors of unique_ptr and share_ptr for the sake of the question, but not aiming to propose any change to their current specifications. Though it would usually be advisable to use make_unique and make_shared , both unique_ptr and shared_ptr can be constructed from a raw pointer. Both get the pointer by value and copy it. Both allow (i.e. in the sense of: do not prevent ) a continuance usage of the original pointer

Overload on reference, versus sole pass-by-value + std::move?

佐手、 提交于 2020-01-30 13:48:12
问题 It seems the main advice concerning C++0x's rvalues is to add move constructors and move operators to your classes, until compilers default-implement them. But waiting is a losing strategy if you use VC10, because automatic generation probably won't be here until VC10 SP1, or in worst case, VC11. Likely, the wait for this will be measured in years. Here lies my problem. Writing all this duplicate code is not fun. And it's unpleasant to look at. But this is a burden well received, for those

Rvalue ref and perfect forwarding

时间秒杀一切 提交于 2020-01-24 06:01:48
问题 I've read few papers about && and I'm just curious if having: void fnc_1(int&& p) { //... } void fnc(int&& r) { fnc_1(r);//am I suppose to/should I? call it like so:fnc_1(std::forward(r)) } or just passing 'r' is enough? 回答1: fnc_1(r) won't compile, because r is an lvalue, just like any other variable, regardless of type. Yes, that's right, named rvalue references are lvalues, not rvalues. fnc_1(std::forward(r)) also won't compile, because std::forward is specifically designed not to infer

Rvalue ref and perfect forwarding

前提是你 提交于 2020-01-24 06:01:46
问题 I've read few papers about && and I'm just curious if having: void fnc_1(int&& p) { //... } void fnc(int&& r) { fnc_1(r);//am I suppose to/should I? call it like so:fnc_1(std::forward(r)) } or just passing 'r' is enough? 回答1: fnc_1(r) won't compile, because r is an lvalue, just like any other variable, regardless of type. Yes, that's right, named rvalue references are lvalues, not rvalues. fnc_1(std::forward(r)) also won't compile, because std::forward is specifically designed not to infer

Rvalue ref and perfect forwarding

≯℡__Kan透↙ 提交于 2020-01-24 06:01:08
问题 I've read few papers about && and I'm just curious if having: void fnc_1(int&& p) { //... } void fnc(int&& r) { fnc_1(r);//am I suppose to/should I? call it like so:fnc_1(std::forward(r)) } or just passing 'r' is enough? 回答1: fnc_1(r) won't compile, because r is an lvalue, just like any other variable, regardless of type. Yes, that's right, named rvalue references are lvalues, not rvalues. fnc_1(std::forward(r)) also won't compile, because std::forward is specifically designed not to infer

C++ move constructor not called for rvalue reference [duplicate]

笑着哭i 提交于 2020-01-23 12:26:46
问题 This question already has answers here : On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule (3 answers) Closed 3 years ago . class MyClass { public: MyClass() { std::cout << "default constructor\n"; } MyClass(MyClass& a) { std::cout << "copy constructor\n"; } MyClass(MyClass&& b) { std::cout << "move constructor\n"; } }; void test(MyClass&& temp) { MyClass a(MyClass{}); // calls MOVE constructor as expected MyClass b(temp); // calls COPY constructor... not expected...? }

Swapping with rvalues

我们两清 提交于 2020-01-22 13:37:28
问题 Suppose I want swap that works on rvalues, and don't want to write 4 versions for all combinations of rvalue/lvalue references (rvalue/rvalue version is kinda pointless but it doesn't hurt). I came up with this: template <typename A, typename B> struct is_same_no_ref : std::is_same< typename std::remove_reference<A>::type, typename std::remove_reference<B>::type > {}; template <typename A, typename B, typename = typename std::enable_if<is_same_no_ref<A, B>::value>::type > inline void my_swap

Swapping with rvalues

北城余情 提交于 2020-01-22 13:37:05
问题 Suppose I want swap that works on rvalues, and don't want to write 4 versions for all combinations of rvalue/lvalue references (rvalue/rvalue version is kinda pointless but it doesn't hurt). I came up with this: template <typename A, typename B> struct is_same_no_ref : std::is_same< typename std::remove_reference<A>::type, typename std::remove_reference<B>::type > {}; template <typename A, typename B, typename = typename std::enable_if<is_same_no_ref<A, B>::value>::type > inline void my_swap

Can I use rvalue reference to temporary? Is it undefined behavior or not?

删除回忆录丶 提交于 2020-01-12 06:50:09
问题 Updating the question Why this two rvalue references examples have different behavior?: Source code: int a = 0; auto && b = a++; ++a; cout << a << b << endl; prints 20 Is it undefined behavior (UB) to use b after the a++ call? Maybe we cannot use b because it refers to a temporary? 回答1: No it is not undefined behavior (UB). It's fine - you can modify the contents of the temporary here (so long as the reference is valid for the lifetime of the temporary, in this case the bind to the rvalue

Does the C++ standard guarantee that a failed insertion into an associative container will not modify the rvalue-reference argument?

半世苍凉 提交于 2020-01-10 11:52:17
问题 #include <set> #include <string> #include <cassert> using namespace std::literals; int main() { auto coll = std::set{ "hello"s }; auto s = "hello"s; coll.insert(std::move(s)); assert("hello"s == s); // Always OK? } Does the C++ standard guarantee that a failed insertion into an associative container will not modify the rvalue-reference argument? 回答1: Explicit and unequivocal NO . Standard doesn't have this guarantee, and this is why try_emplace exists. See notes: Unlike insert or emplace,