A a; A& a_ref1 = a; // lvalue
A a; A&& a_ref2 = a; // rvalue
A& a_ref3 = A(); // ! A&& a_ref4 = A(); // Ok
v2 = v1
, it usually causes a function call, memory allocation and a loop. This, of course, is acceptable when we really need two copies of a vector, but in many cases this is not so: we often copy a vector from one place to another, and then delete the old copy. Consider: template <class T> swap(T& a, T& b) { T tmp(a); // a a = b; // b b = tmp; // tmp (.. a) }
a
or b
, we just wanted to exchange them. Let's try again: template <class T> swap(T& a, T& b) { T tmp(std::move(a)); a = std::move(b); b = std::move(tmp); }
move()
returns the value of the object passed as a parameter, but does not guarantee the safety of this object. For example, if we pass vector
as a parameter in move()
, then we can reasonably expect that after the function works, the parameter will have a zero-length vector, since all elements will be moved rather than copied. In other words, a move is an erase read (destructive read).swap
specialization. However, we cannot specialize every function that copies a large object just before it deletes or overwrites it. It would be non-constructive.move
function actually performs a very modest job. Its task is to accept either the lvalue or the rvalue parameter, and return it as an rvalue without calling the copy constructor: template <class T> typename remove_reference<T>::type&& move(T&& a) { return a; }
clone_ptr
could own a pointer and call its expensive clone()
method to copy: template <class T> class clone_ptr { private: T* ptr; public: // explicit clone_ptr(T* p = 0) : ptr(p) {} // ~clone_ptr() {delete ptr;} // clone_ptr(const clone_ptr& p) : ptr(p.ptr ? p.ptr->clone() : 0) {} clone_ptr& operator=(const clone_ptr& p) { if (this != &p) { delete ptr; ptr = p.ptr ? p.ptr->clone() : 0; } return *this; } // clone_ptr(clone_ptr&& p) : ptr(p.ptr) {p.ptr = 0;} clone_ptr& operator=(clone_ptr&& p) { std::swap(ptr, p.ptr); return *this; } // T& operator*() const {return *ptr;} // ... };
clone_ptr
is the code that can be found in today's C ++ books. Users could use clone_ptr
like this: clone_ptr<base> p1(new derived); // ... clone_ptr<base> p2 = p1; // p2 p1
clone_ptr
is a relatively expensive operation. However, when the source of the copy is an rvalue, you can avoid calling the potentially expensive clone()
operation by stealing the rvalue pointer (no one will notice!). In the semantics of relocation, the relocation constructor leaves the value of rvalue in the created object, and the assignment operator reverses the values of the current object with the rvalue object of the link.clone_ptr
, or if there is explicit permission to read the source of the rvalue copy (using std::move
), the work will be done much faster. clone_ptr<base> p1(new derived); // ... clone_ptr<base> p2 = std::move(p1); // p2 , p1
std::move
function. class Derived : public Base { std::vector<int> vec; std::string name; // ... public: // ... // Derived(Derived&& x) // rvalue : Base(std::move(x)), vec(std::move(x.vec)), name(std::move(x.name)) { } Derived& operator=(Derived&& x) // rvalue { Base::operator=(std::move(x)); vec = std::move(x.vec); name = std::move(x.name); return *this; } // ... };
std::vector
and std::string
, move operations are already implemented (just like our clone_ptr
), which allows you to avoid much more expensive copy operations.x
parameter is treated as an lvalue in move operations, despite the fact that it is declared as a rvalue reference. Therefore, you must use move(x)
instead of just x
when passing to the base class. This is a key security mechanism for semantics of motion, designed to prevent accidental double-move attempts from some named variable. All movements occur only from rvalues or with an explicit reduction to rvalue (with std::move
). If the variable has a name, then it is an lvalue.std::complex
?)fstream
unique_ptr
(not shared and not copied ownership) ifstream find_and_open_data_file(/* ... */); ... ifstream data_file = find_and_open_data_file(/* ... */); // !
ifstream
source is rvalue. There is only one file descriptor at any given time, and only ifstream
owns it.vector
relocation), it will simply move it instead of copying. vector<unique_ptr<base>> v1, v2; v1.push_back(unique_ptr<base>(new derived())); // OK, ... v2 = v1; // ! . v2 = move(v1); // . v2.
std::swap
, the implementation of which is described above), but also allows these algorithms to work with non-copied (but relocatable) types. For example, the following code sorts vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
vector<unique_ptr>, , :
struct indirect_less { template <class T> bool operator()(const T& x, const T& y) {return *x < *y;} }; ... std::vector<std::unique_ptr<A>> v; ... std::sort(v.begin(), v.end(), indirect_less());
unique_ptr
, swap
( , ) / . , , . ( , ), .
(perfect forwarding)
, std::shared_ptr
. . , , . :
template <class T> std::shared_ptr<T> factory() // { return std::shared_ptr<T>(new T); } template <class T, class A1> std::shared_ptr<T> factory(const A1& a1) // { return std::shared_ptr<T>(new T(a1)); } //
. :
std::shared_ptr<A> p = factory<A>(5);
: , T
?
, factory
T
.
factory
:
template <class T, class A1> std::shared_ptr<T> factory(A1& a1) { return std::shared_ptr<T>(new T(a1)); }
. const
factory
, (, A1
) T
. , , T
. (, std::bind
).
:
std::shared_ptr<A> p = factory<A>(5); // ! A* q = new A(5); // OK
factory
, "5" factory
, int&
rvalue "5". , , .
: AI
& const AI
&?
, : 4 . 8 . 16 .. .
Rvalue :
template <class T, class A1> std::shared_ptr<T> factory(A1&& a1) { return std::shared_ptr<T>(new T(std::forward<A1>(a1))); }
rvalue factory
. const
, factory
.
: forward
?
move
, forward
- , , , . a1
, .
, forward
lvalue/rvalue , factory
. factory
rvalue, forward
T
rvalue. , lvalue factory
, T
lvalue.
forward
:
template <class T> struct identity { typedef T type; }; template <class T> T&& forward(typename identity<T>::type&& a) { return a; }
, . , 95% .
, , , , N1377 .
N1385 .
rvalue ( ), N1690 .
, rvalue , N1952 .
rvalue , N2027 .
rvalue , N1771 .
, rvalue , :
N1856 N1857 N1858 N1859 N1860 N1861 N1862
rvalue (this), N1821 .
Source: https://habr.com/ru/post/226229/
All Articles