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: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,

Pass by reference, constant reference, rvalue-reference, or constant rvalue-reference?

若如初见. 提交于 2020-01-10 10:32:14
问题 I was learning passing by reference, and here is the test I did: #include <iostream> using namespace std; int i = 0; //If this is uncommented, compiler gives ambiguous definition error. //void paramCheck (string s) { // cout << ++i << ". Param is var.\n"; //} void paramCheck (const string& s) { cout << ++i << ". Param is const ref.\n"; } void paramCheck (string& s) { cout << ++i << ". Param is non-const ref.\n"; } void paramCheck (const string&& s) { cout << ++i << ". Param is const rvalue

Pass by reference, constant reference, rvalue-reference, or constant rvalue-reference?

微笑、不失礼 提交于 2020-01-10 10:30:05
问题 I was learning passing by reference, and here is the test I did: #include <iostream> using namespace std; int i = 0; //If this is uncommented, compiler gives ambiguous definition error. //void paramCheck (string s) { // cout << ++i << ". Param is var.\n"; //} void paramCheck (const string& s) { cout << ++i << ". Param is const ref.\n"; } void paramCheck (string& s) { cout << ++i << ". Param is non-const ref.\n"; } void paramCheck (const string&& s) { cout << ++i << ". Param is const rvalue

binding a lvalue expression of type T&&

霸气de小男生 提交于 2020-01-05 07:06:15
问题 In the last few days I've been trying to grasp an apparently trivial principle behind lvalue/rvalue references. Let us define a new rvalue reference: int&& x = 12; x is therefore an lvalue expression of type int&& . Since x is a lvalue, it can be bound to a lvalue reference of the same type, i.e., a lvalue reference of type int&& . Such a lvalue reference would be defined as: int&& & ref_x = x; // non-working code, just for the sake of explanation Of course, it is not possible to explicitly

Why would const-ness of a local variable inhibit move semantics for the returned value?

人盡茶涼 提交于 2020-01-02 00:41:31
问题 struct STest : public boost::noncopyable { STest(STest && test) : m_n( std::move(test.m_n) ) {} explicit STest(int n) : m_n(n) {} int m_n; }; STest FuncUsingConst(int n) { STest const a(n); return a; } STest FuncWithoutConst(int n) { STest a(n); return a; } void Caller() { // 1. compiles just fine and uses move ctor STest s1( FuncWithoutConst(17) ); // 2. does not compile (cannot use move ctor, tries to use copy ctor) STest s2( FuncUsingConst(17) ); } The above example illustrates how in C+

Difference between returning a const reference and rvalue reference

余生长醉 提交于 2020-01-01 05:03:11
问题 If I'm not wrong, I think that both a const reference and a rvalue reference can bind to a rvalue. Is there any practical difference between a function that returns the former and a function that returns the latter? EDIT. I cannot modify the former, but why would I be interested in modifying a rvalue? Does it make sense? 回答1: A const lvalue reference can bind to anything. An rvalue reference can only bind to non- const rvalues. non-const lvalue const lvalue non-const rvalue const rvalue const

Move Semantics and Pass-by-Rvalue-Reference in Overloaded Arithmetic

删除回忆录丶 提交于 2020-01-01 04:23:08
问题 I am coding a small numeric analysis library in C++. I have been trying to implement using the latest C++11 features including move semantics. I understand the discussion and top answer at the following post: C++11 rvalues and move semantics confusion (return statement) , but there is one scenario that I still am trying to wrap my head around. I have a class, call it T , which is fully equipped with overloaded operators. I also have both copy and move constructors. T (const T &) { /

C++11 lambda can be assigned to std::function with incorrect signature

孤者浪人 提交于 2020-01-01 04:01:08
问题 The following compiles and runs (under Apple LLVM version 6.1.0 and Visual C++ 2015): #include <functional> #include <iostream> struct s { int x; }; int main(int argc, char **argv) { std::function<void (s &&)> f = [](const s &p) { std::cout << p.x; }; f(s {1}); return 0; } Why doesn't the assignment std::function<void (s &&)> f = [](const s &p) { std::cout << p.x; }; generate an error? A function accepting an rvalue reference should not have the same signature as a function accepting a const

Intuitive understanding of functions taking references of references [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-31 09:03:14
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: What does T&& mean in C++11? For some reason, this is eluding my intuition, and I cannot find any explanation on the internet. What does it mean for a C++ function to take a reference of a reference? For example: void myFunction(int&& val); //what does this mean?! I understand the idea of passing-by-reference, so void addTwo(int& a) { a += 2; } int main() { int x = 5; addTwo(x); return 0; } works and is

Workarounds for no 'rvalue references to *this' feature

谁说我不能喝 提交于 2019-12-31 08:12:08
问题 I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy itself is being moved. I believe that I will be able to implement this behaviour as per proposal n2439 "Extending move semantics to *this", but it is not yet available in a release of gcc and won't be for a while. The code below is what I am ultimately aiming for, but is not currently possible. Until this feature is