Yes, adding a unique_ptr<int>
member would make your class noncopyable. But that's hardly the best solution. You just added 8 bytes to Foo
, 8 bytes that didn't need to exist if you just did:
class Foo {
Foo(const Foo&) = delete;
Foo& operator=(const Foo&) = delete;
};
Furthermore, the compile errors when you try to copy accidentally will be much clearer. Consider the latest gcc:
main.cpp:17:13: error: use of deleted function 'Foo::Foo(const Foo&)'
Foo g = f;
^
main.cpp:9:5: note: declared here
Foo(const Foo&) = delete;
^
As opposed to:
main.cpp:17:13: error: use of deleted function 'Foo::Foo(const Foo&)'
Foo g = f;
^
main.cpp:6:8: note: 'Foo::Foo(const Foo&)' is implicitly deleted because the default definition would be ill-formed:
struct Foo {
^
main.cpp:6:8: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]'
In file included from /usr/local/include/c++/5.1.0/memory:81:0,
from main.cpp:4:
/usr/local/include/c++/5.1.0/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^