VS-2013. , , , , , ( ).
Foo:
class Foo
{
public:
Foo(std::unique_ptr<A> ref) : mRef(std::move(ref)) {}
Foo(Foo&& other) : mRef(std::move(other.mRef)) {}
Foo(const Foo& other) {}
Foo& operator=(const Foo& other) { return *this; }
friend std::ostream&
operator<<(std::ostream& os, const Foo& f)
{
if (f.mRef)
os << *f.mRef;
else
os << "nullptr";
return os;
}
protected:
std::unique_ptr<A> mRef;
};
main:
: / A, , .
int main(int argc, char *argv[])
{
std::vector<Foo> v({ Foo(std::make_unique<A>(1)), Foo(std::make_unique<A>(2)) });
for (const auto& p : v)
std::cout << p << '\n';
}
:
nullptr
nullptr
, , .
initializer_list, , , vector Foo copy, unique_ptr.
, Foo, , ( ), .
, Foo . , - :
Foo(const Foo& other)
: mRef(other.mRef ? new A(*other.mRef) : nullptr)
{}
, , , . , , .
VS-2013.
, . , = default. , noexcept. . vector<Foo>.
, VS-2013 , noexcept.
VS-2013 , . , VS-2015 . , {}.
:
class Foo
{
public:
Foo(std::unique_ptr<A> ref) : mRef(std::move(ref)) {}
Foo(Foo&& other) : mRef(std::move(other.mRef)) {}
Foo(const Foo& other) : mRef(other.mRef->clone()) {}
Foo& operator=(const Foo& other) { mRef = other.mRef->clone(); return *this; }
protected:
std::unique_ptr<A> mRef;
};
nullptr -dereference. other , ->clone() a nullptr. , , . .