rvalue-reference

How is it possible to get a reference to an rvalue?

。_饼干妹妹 提交于 2019-11-30 09:35:24
I have used std::move and std::forward in C++. My question is: how are these functions actually implemented by the standard library? If an lvalue is something you can get the address of, and an rvalue is exclusively not an lvalue, how can you actually implement these references? Do these new facilities allow for something like: auto x = &(3); or something like that? Can you get a reference to an rvalue that isn't just a std::move / forward returned lvalue? Hopefully these questions make sense. I couldn't find good information on Google, just tutorials on perfect forwarding, etc. Jonathan Mee I

Lifetime extension and the conditional operator

情到浓时终转凉″ 提交于 2019-11-30 08:09:14
local lvalue references-to-const and rvalue references can extend the lifetime of temporaries: const std::string& a = std::string("hello"); std::string&& b = std::string("world"); Does that also work when the initializer is not a simple expression, but uses the conditional operator? std::string&& c = condition ? std::string("hello") : std::string("world"); What if one of the results is a temporary object, but the other one isn't? std::string d = "hello"; const std::string& e = condition ? d : std::string("world"); Does C++ mandate the lifetime of the temporary be extended when the condition is

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

Understanding rvalue references

纵饮孤独 提交于 2019-11-30 07:07:19
问题 I think there's something I'm not quite understanding about rvalue references. Why does the following fail to compile (VS2012) with the error 'foo' : cannot convert parameter 1 from 'int' to 'int &&' ? void foo(int &&) {} void bar(int &&x) { foo(x); }; I would have assumed that the type int && would be preserved when passed from bar into foo. Why does it get transformed into int once inside the function body? I know the answer is to use std::forward : void bar(int &&x) { foo(std::forward<int>

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

谁说我不能喝 提交于 2019-11-30 06:56:02
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-reference.\n"; } void paramCheck (string&& s) { cout << ++i << ". Param is non-const rvalue-reference.\n"

Perfect forwarding for void and non-void returning functions

谁说胖子不能爱 提交于 2019-11-30 06:38:25
问题 Previously I was using a macro to measure the time a function call took whenever I wanted to quickly check that. Now, with C++11 available, I would like to finally remove that ugly peace of preprocessor code and replace it with something like this: template <typename Functor, typename ... Args> auto measure(Functor f, Args && ... args) -> decltype(f(std::forward<Args>(args)...)) { auto now = std::chrono::high_resolution_clock::now(); auto ret = f(std::forward<Args>(args)...); auto elapsed =

How does std::forward receive the correct argument?

不问归期 提交于 2019-11-30 06:38:10
问题 Consider: void g(int&); void g(int&&); template<class T> void f(T&& x) { g(std::forward<T>(x)); } int main() { f(10); } Since the id-expression x is an lvalue, and std::forward has overloads for lvalues and rvalues, why doesn't the call bind to the overload of std::forward that takes an lvalue? template<class T> constexpr T&& forward(std::remove_reference_t<T>& t) noexcept; 回答1: It does bind to the overload of std::forward taking an lvalue: template <class T> constexpr T&& forward(remove

What is an rvalue reference to function type?

谁都会走 提交于 2019-11-30 03:57:52
I have recently wrapped my mind around the C++0x's concepts of glvalues, xvalues and prvalues, as well as the rvalue references. However, there's one thing which still eludes me: What is "an rvalue reference to function type" ? It is literally mentioned many times in the drafts. Why was such a concept introduced? What are the uses for it? I hate to be circular, but an rvalue reference to function type is an rvalue reference to function type. There is such a thing as a function type, e.g. void () . And you can form an rvalue reference to it. In terms of the classification system introduced by

c++11 emplace_back and push_back syntax with struct

独自空忆成欢 提交于 2019-11-30 01:55:28
I'm using MSVC, Visual Studio 2013. Suppose I have a struct: struct my_pair { int foo, bar; }; And I want to add a bunch of these efficiently, without too creating a temporary and then discarding it: vector<my_pair> v; v.push_back(41, 42); // does not work [a] v.push_back({41,42}); // works [b] v.emplace_back(41,42); // does not work [c] v.emplace_back({41,42}); // does not work [d] v.emplace_back(my_pair{41,42}); //works [e] Now if I add a constructor and copy constructor to my code: my_pair(int foo_, int bar_) : foo(foo_), bar(bar_) { cout << "in cstor" << endl; } my_pair(const my_pair& copy

Numeric vector operator overload+ rvalue reference parameter

落爺英雄遲暮 提交于 2019-11-29 18:29:50
问题 I have the numeric vector template class below (vector for numerical calculations). I am trying make it possible to write D=A+B+C where all variables are Vector objects. A , B and C should not be modified. My idea is to use Vector operator+(Vector&& B) so that after (hopefully) an Rvalue Vector has been returned from B+C , all subsequent additions are stored in that object i.e. steal the storage of the Rvalue for all subsequent additions. This is in order to eliminate creation of new objects