pass-by-rvalue-reference

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,

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

夙愿已清 提交于 2020-01-10 11:51:28
问题 #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,

How to override “no non-const reference to temporary object” correctly

最后都变了- 提交于 2019-12-13 03:58:42
问题 I have a class Foo . Foo has a few non-const methods. I am okay with calling non-const methods on a temporary Foo object; I am only interested in what the methods actually return, than what they do to the Foo object itself. First question: Does this, by itself, necessary indicate that the class Foo is not well-designed? Second question: If I want to continue with Foo as-is, but I still want to be able to pass Foo objects by reference to functions that will call non-const methods on it, what

Return rvalue reference vs return by value in function return type [duplicate]

南笙酒味 提交于 2019-12-10 21:42:37
问题 This question already has answers here : c++11 Return value optimization or move? [duplicate] (4 answers) Closed 4 years ago . In my code I have a function that constructs a string from a piece of data and then returns it. This string isn't used anywhere else, so it's safe for the receiving side to use move-assignment or move-initialization on it. std::string ReadString(...) { ... return std::string(...) } This is basically what I have. Is there any point in making the function return type

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

耗尽温柔 提交于 2019-11-30 07:54:16
#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? SergeyA Explicit and unequivocal NO . Standard doesn't have this guarantee, and this is why try_emplace exists. See notes: Unlike insert or emplace, these functions do not move from rvalue arguments if the insertion does not happen , which makes it

Returning an argument passed by rvalue reference

流过昼夜 提交于 2019-11-30 07:33:13
问题 If I have a class A and functions A f(A &&a) { doSomething(a); return a; } A g(A a) { doSomething(a); return a; } the copy constructor is called when returning a from f , but the move constructor is used when returning from g . However, from what I understand, f can only be passed an object that it is safe to move (either a temporary or an object marked as moveable, e.g., using std::move ). Is there any example when it would not be safe to use the move constructor when returning from f ? Why

Returning an argument passed by rvalue reference

帅比萌擦擦* 提交于 2019-11-29 04:36:15
If I have a class A and functions A f(A &&a) { doSomething(a); return a; } A g(A a) { doSomething(a); return a; } the copy constructor is called when returning a from f , but the move constructor is used when returning from g . However, from what I understand, f can only be passed an object that it is safe to move (either a temporary or an object marked as moveable, e.g., using std::move ). Is there any example when it would not be safe to use the move constructor when returning from f ? Why do we require a to have automatic storage duration? I read the answers here , but the top answer only