rvalue-reference

When does returning an rvalue reference result in undefined behavior?

為{幸葍}努か 提交于 2019-12-10 21:29:34
问题 In an Stack Overflow answer here, Kerrek posts the following code. Foo && g() { Foo y; // return y; // error: cannot bind ‘Foo’ lvalue to ‘Foo&&’ return std::move(y); // OK type-wise (but undefined behaviour, thanks @GMNG) } GManNickG points out that this results in undefined behavior. Kerrek adds True; you don't really return && from anything but forward and move. It's a contrived example. What confuses me is that the C++11 standard uses a function call that returns an rvalue reference as an

Passing an Lvalue to a parameter of RValue

做~自己de王妃 提交于 2019-12-10 16:53:14
问题 I wanted to know how this is possible ? template<typename T> void Test(T&& arg) { arg = 14; } int a = 23; Test(a); My question is that the function Test requires an argument of type Rvalue however it seems to also accept parameter of type lvalue. Why is that ? Is that because of presence of templates ? Because If i do something like this void AnotherTest(int&& arg) { arg = 14; } Then the function requires the parameter to be of type Rvalue. I would appreciate it if someone could explain why

Debugging C++11 rvalue references with gdb

倖福魔咒の 提交于 2019-12-10 01:57:20
问题 I just noticed that I can not debug rvalue references with gdb-7.7.1 properly. void simple(int &&i) {} When I enter this minimalistic function I can not obtain any meaningful information about i . It's type and value are unknown to gdb . simple(int&&) (i=<unknown type in /tmp/test, CU 0x0, DIE 0xcd78>) at test.cpp:10 (gdb) p i $2 = <unknown type in /tmp/test, CU 0x0, DIE 0xcd78> Am I doing something wrong? Are there any sensible workarounds? Will upgrading to gdb-7.10 solve this issue ? 回答1:

Temporary const array not binding to rvalue reference

六月ゝ 毕业季﹏ 提交于 2019-12-10 01:52:38
问题 I have the following test program: #include <iostream> #include <type_traits> #include <utility> template<typename Ty, std::size_t N> void foo(Ty (&&)[N]) { std::cout << "Ty (&&)[" << N << "]\t" << std::is_const<Ty>::value << '\n'; } template<typename Ty, std::size_t N> void foo(Ty (&)[N]) { std::cout << "Ty (&)[" << N << "]\t" << std::is_const<Ty>::value << '\n'; } template<typename Ty> using id = Ty; int main() { std::cout.setf(std::cout.boolalpha); foo(id<int[]>{1, 2, 3, 4, 5}); foo(id<int

Take ownership of parameter by rvalue-reference

筅森魡賤 提交于 2019-12-10 01:16:15
问题 I want to make clear that the constructor of my class A will take ownership of the passed Data parameter. The obvious thing to do is take a unique_ptr by value: class A { public: A(std::unique_ptr<Data> data) : _data(std::move(data)) { } std::unique_ptr<Data> _data; }; However, for my use-case, there is no reason why Data should be a pointer, since a value type would suffice. The only remaining option that I could think of to make really clear that Data will be owned by A is pass by rvalue

rvalue reference template deduction

我的梦境 提交于 2019-12-09 18:41:10
问题 template<class U> void f( U && v) { std::cout << typeid(v).name() << "\n"; //'int' in both cases if( boost::is_same<int&&,U>::value ) { std::cout << "reach here\n"; //only with f<int&&>(int(1)); } } int main() { f(int(1)); f<int&&>(int(1)); std::cin.ignore(); } Why v parameter is interpreted as int when I don't explicitly use f<int&&> ? What is the difference ? (Compiled with MVS2010) My guess is that First is passed as a rvalue and second as a rvalue reference and both bound correctly into a

Arrays and Rvalues (as parameters)

只愿长相守 提交于 2019-12-09 17:27:49
问题 I wonder if there is any way to differentiate the function calls (with arrays as parameters) shown in the following code: #include <cstring> #include <iostream> template <size_t Size> void foo_array( const char (&data)[Size] ) { std::cout << "named\n"; } template <size_t Size> void foo_array( char (&&data)[Size] ) //rvalue of arrays? { std::cout << "temporary\n"; } struct A {}; void foo( const A& a ) { std::cout << "named\n"; } void foo( A&& a ) { std::cout << "temporary\n"; } int main( /*

Why can't I return a unique_ptr from a pair?

淺唱寂寞╮ 提交于 2019-12-09 17:23:45
问题 Why can't I return a unique_ptr from a pair? #include <iostream> #include <memory> #include <utility> using namespace std; unique_ptr<int> get_value() { pair<unique_ptr<int>, int> p(unique_ptr<int>(new int(3)), 4); return p.first; } int main(void) { cout << *get_value() << endl; return 0; } When I try to compile this with g++ 4.6, I get: ../main.cpp: In function ‘std::unique_ptr<int> get_value()’: ../main.cpp:9:11: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const

rvalue function overloading

喜你入骨 提交于 2019-12-09 13:15:43
问题 I want to overload a function so that it manipulates its argument in some way and then returns a reference to the argument – but if the argument is not mutable, then it should return a manipulated copy of the argument instead. After messing around with it for ages, here's what I've come up with. using namespace std; string& foo(string &in) { in.insert(0, "hello "); return in; } string foo(string &&in) { return move(foo(in)); } string foo(const string& in) { return foo(string(in)); } This code

Some clarification on rvalue references

五迷三道 提交于 2019-12-09 05:50:48
问题 First: where are std::move and std::forward defined? I know what they do, but I can't find proof that any standard header is required to include them. In gcc44 sometimes std::move is available, and sometimes its not, so a definitive include directive would be useful. When implementing move semantics, the source is presumably left in an undefined state. Should this state necessarily be a valid state for the object? Obviously, you need to be able to call the object's destructor, and be able to