rvalue-reference

In c++11 why not right to use moved variable after std::move?

Deadly 提交于 2020-08-25 02:35:50
问题 As std::move(v) is merely a cast like static_cast<T&&>(v) where T is the type of v . But why the moved variable not a reference of the original object? For example, when o1 is "moved" to o2 , why can't we access the str_ of o2 through o1.str_ any more? #include <string> #include <iostream> struct MyClass { std::string str_; MyClass(MyClass const &) = delete; MyClass &operator=(MyClass const &) = delete; MyClass(MyClass &&o) : str_(std::move(o.str_)) {} MyClass(std::string const &str) : str_

Exact correspondence between r-value references and pointers?

青春壹個敷衍的年華 提交于 2020-08-05 13:31:43
问题 This is a general question about symmetry between pointer and reference types in the C++ language. Is this table of correspondence meaningful in C++ (C++11 and beyond)? +-----------+----------------+ | Reference | Pointer | |-----------|----------------| | T& | T* const | | T const& | T const* const | | T&& | ???* | +-----------+----------------+ and if some, what would correspond to ???* ?. (Any additional rows are missing?) ( T&& is for a concrete type T , not a deduced type. See

Why do we need reference collapsing rules [duplicate]

纵然是瞬间 提交于 2020-07-20 04:43:00
问题 This question already has answers here : Concise explanation of reference collapsing rules requested: (1) A& & -> A& , (2) A& && -> A& , (3) A&& & -> A& , and (4) A&& && -> A&& (2 answers) Closed 6 years ago . Everyone may know that we use rvalue-references combined with the reference collapsing rules to build perfect forwarding functions, like so template<typename T> void f(T&& arg) { otherfunc(std::forward<T>(arg)); } f(4); The reference collapsing rules are like +------+-----+--------+ | T

Why do we need reference collapsing rules [duplicate]

自闭症网瘾萝莉.ら 提交于 2020-07-20 04:42:49
问题 This question already has answers here : Concise explanation of reference collapsing rules requested: (1) A& & -> A& , (2) A& && -> A& , (3) A&& & -> A& , and (4) A&& && -> A&& (2 answers) Closed 6 years ago . Everyone may know that we use rvalue-references combined with the reference collapsing rules to build perfect forwarding functions, like so template<typename T> void f(T&& arg) { otherfunc(std::forward<T>(arg)); } f(4); The reference collapsing rules are like +------+-----+--------+ | T

Why do we need reference collapsing rules [duplicate]

↘锁芯ラ 提交于 2020-07-20 04:42:05
问题 This question already has answers here : Concise explanation of reference collapsing rules requested: (1) A& & -> A& , (2) A& && -> A& , (3) A&& & -> A& , and (4) A&& && -> A&& (2 answers) Closed 6 years ago . Everyone may know that we use rvalue-references combined with the reference collapsing rules to build perfect forwarding functions, like so template<typename T> void f(T&& arg) { otherfunc(std::forward<T>(arg)); } f(4); The reference collapsing rules are like +------+-----+--------+ | T

Rvalue reference parameters and template functions

Deadly 提交于 2020-07-04 13:48:47
问题 If I define a function which accepts an rvalue reference parameter: template <typename T> void fooT(T &&x) {} I can call it, using GCC 4.5, with either a , ar , or arr : int a, &ar = a, &&arr = 7; fooT(a); fooT(ar); fooT(arr); However, calling a similar, non-template function, void fooInt(int &&x) {} with any of those three arguments will fail. I was preparing to strengthen my knowledge of forward , but this has knocked me off course. Perhaps it's GCC 4.5; I was surprised to find that the

Assign a value to an rvalue reference returned from function

牧云@^-^@ 提交于 2020-06-27 07:17:32
问题 #include <utility> template <typename Container> decltype(auto) index(Container &&arr, int n) { return std::forward<Container>(arr)[n]; } Make a function call : #include <vector> index(std::vector {1, 2, 3, 4, 5}, 2) = 0; When function calling finished, the object std::vector {1, 2, 3, 4, 5} will be destroyed, assigning a value to a deallocated address would cause undefined behaviour. But the above code works well and valgrind detected nothing. Maybe the compile helps me make another

Move assignable class containing vector<unique_ptr<T>>

拈花ヽ惹草 提交于 2020-06-17 06:00:08
问题 The class Foo has an rvalue reference constructor that moves the contained vector of unique_ptr s so why does the following code give the following error, both with or without the std::move on the Foo() in main ? error C2280: 'std::unique_ptr<SomeThing,std::default_delete<_Ty>> &std::unique_ptr<_Ty,std::default_delete<_Ty>>::operator =(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function class Foo{ public: Foo(){ } Foo(Foo&& other) : m_bar(std:

Does it make sense to use the move-and-swap idiom on a movable and non-copyable class

落花浮王杯 提交于 2020-05-12 21:20:05
问题 If I have a class such as class Foo{ public: Foo(){...} Foo(Foo && rhs){...} operator=(Foo rhs){ swap(*this, rhs);} void swap(Foo &rhs); private: Foo(const Foo&); // snip: swap code }; void swap(Foo& lhs, Foo& rhs); Does it make sense to implement operator= by value and swap if I don't have a copy constructor? It should prevent copying my objects of class Foo but allow moves. This class is not copyable so I shouldn't be able to copy construct or copy assign it. Edit I've tested my code with