noncopyable

How to create a container of noncopyable elements

梦想与她 提交于 2019-12-01 17:08:49
Is there a way use STL containters with non-copyable elements? something like this: class noncopyable { noncopyable(noncopyable&); const noncopyable& operator=(noncopyable&); public: noncopyable(){}; }; int main() { list<noncopyable> MyList; //error C2248: 'noncopyable::noncopyable' : cannot access private member declared in class 'noncopyable' } No, non-copyable elements can't be in C++ container classes. According to the standard, 23.1 paragraph 3, "The type of objects stored in these components must met the requirements of CopyConstructible types (20.1.3), and the additional requirements of

Repeating Q_DISABLE_COPY in QObject derived classes

走远了吗. 提交于 2019-12-01 02:37:05
In Qt there is a macro that allows declaring private copy constructurs and assignment operators for classes: http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#Q_DISABLE_COPY It is said that this macro should be used for all QObject (especially QWidget) derived classes. I understand how this works and why it is useful. What I do not understand: Is there any reason to repeat Q_DISABLE_COPY in my QObject derived classes while QObject already contains Q_DISABLE_COPY and through this effectively prevents my derived classes from being copied? The error message that might be printed from

std::map<>::insert using non-copyable objects and uniform initialization

拈花ヽ惹草 提交于 2019-11-27 23:38:59
Have a look at the following code: #include <utility> #include <map> // non-copyable but movable struct non_copyable { non_copyable() = default; non_copyable(non_copyable&&) = default; non_copyable& operator=(non_copyable&&) = default; // you shall not copy non_copyable(const non_copyable&) = delete; non_copyable& operator=(const non_copyable&) = delete; }; int main() { std::map<int, non_copyable> map; //map.insert({ 1, non_copyable() }); < FAILS map.insert(std::make_pair(1, non_copyable())); // ^ same and works } Compiling this snippet fails when uncommenting the marked line on g++ 4.7. The

How do I make this C++ object non-copyable?

会有一股神秘感。 提交于 2019-11-27 03:10:27
See title. I have: class Foo { private: Foo(); public: static Foo* create(); } What need I do from here to make Foo un-copyable? Thanks! Klaim class Foo { private: Foo(); Foo( const Foo& ); // non construction-copyable Foo& operator=( const Foo& ); // non copyable public: static Foo* create(); } If you're using boost, you can also inherit from noncopyable : http://www.boost.org/doc/libs/1_41_0/boost/noncopyable.hpp EDIT: C++11 version if you have a compiler supporting this feature: class Foo { private: Foo(); Foo( const Foo& ) = delete; // non construction-copyable Foo& operator=( const Foo& )

With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class?

青春壹個敷衍的年華 提交于 2019-11-27 00:54:52
With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class? I'm talking about the trick where you privately inherit a base class which has private or deleted copy constructor and copy assignment (e.g. boost::noncopyable ). Are the advantages put forward in this question still applicable to C++11? I don't get why some people claim it's easier to make a class non-copyable in C++11. In C++03: private: MyClass(const MyClass&) {} MyClass& operator=(const MyClass&) {} In C++11: MyClass(const MyClass&) = delete; MyClass& operator=(const MyClass&

std::map<>::insert using non-copyable objects and uniform initialization

六月ゝ 毕业季﹏ 提交于 2019-11-26 21:33:25
问题 Have a look at the following code: #include <utility> #include <map> // non-copyable but movable struct non_copyable { non_copyable() = default; non_copyable(non_copyable&&) = default; non_copyable& operator=(non_copyable&&) = default; // you shall not copy non_copyable(const non_copyable&) = delete; non_copyable& operator=(const non_copyable&) = delete; }; int main() { std::map<int, non_copyable> map; //map.insert({ 1, non_copyable() }); < FAILS map.insert(std::make_pair(1, non_copyable()));

What are the advantages of boost::noncopyable

三世轮回 提交于 2019-11-26 19:49:37
To prevent copying a class, you can very easily declare a private copy constructor / assignment operators. But you can also inherit boost::noncopyable . What are the advantages / disadvantages of using boost in this case? I see no documentation benefit: #include <boost/noncopyable.hpp> struct A : private boost::noncopyable { }; vs: struct A { A(const A&) = delete; A& operator=(const A&) = delete; }; When you add move-only types, I even see the documentation as misleading. The following two examples are not copyable, though they are movable: #include <boost/noncopyable.hpp> struct A : private

With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class?

不想你离开。 提交于 2019-11-26 09:29:44
问题 With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class? I\'m talking about the trick where you privately inherit a base class which has private or deleted copy constructor and copy assignment (e.g. boost::noncopyable). Are the advantages put forward in this question still applicable to C++11? I don\'t get why some people claim it\'s easier to make a class non-copyable in C++11. In C++03: private: MyClass(const MyClass&) {} MyClass&

What are the advantages of boost::noncopyable

醉酒当歌 提交于 2019-11-26 07:27:58
问题 To prevent copying a class, you can very easily declare a private copy constructor / assignment operators. But you can also inherit boost::noncopyable . What are the advantages / disadvantages of using boost in this case? 回答1: I see no documentation benefit: #include <boost/noncopyable.hpp> struct A : private boost::noncopyable { }; vs: struct A { A(const A&) = delete; A& operator=(const A&) = delete; }; When you add move-only types, I even see the documentation as misleading. The following