noncopyable

Repeating Q_DISABLE_COPY in QObject derived classes

半世苍凉 提交于 2020-01-11 04:28:08
问题 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

How to create a container of noncopyable elements

核能气质少年 提交于 2019-12-30 18:34:50
问题 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' } 回答1: 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

How to initialize a container of noncopyable with initializer list? [duplicate]

↘锁芯ラ 提交于 2019-12-30 08:06:18
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can I list-initialize a vector of move-only type? I use gcc 4.6.1 to compile this code int main() { std::vector<std::unique_ptr<int>> vec({ std::unique_ptr<int>(new int(0)), std::unique_ptr<int>(new int(1)), }); return 0; } In what g++ complains there is something like /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../include/c++/4.6.1/bits/stl_construct.h:76:7: **error: use of deleted function 'std:

Making a class non-copyable: Private undefined methods vs Deleted methods

旧街凉风 提交于 2019-12-23 14:40:54
问题 Prior to C++11, I saw code like this: class Car { public: Car() {} private: Car(const Car&); Car& operator=(const Car&); }; For C++11 (and later), I see code like this: class Car { public: Car() {} private: Car(const Car&) = delete; Car& operator=(const Car&) = delete; }; Do they behave identically? If not, please explain. Ref: https://ariya.io/2015/01/c-class-and-preventing-object-copy 回答1: They are similar in most respects, but differ in some others. Consider the following external code

Does OpenMP copy private objects?

别等时光非礼了梦想. 提交于 2019-12-23 12:18:49
问题 I'm writing a program that reads huge file (3x280 GB) and does a fitting procedure to the data in the file. It's pretty convenient to parallelise such a program, where this is easily done with OpenMP. The thing I don't understand is how private variables are taken in OpenMP. As we all know, fstream's obejcts are a non-copyable, and intiuitively, that prevented me from using it as a private object. So the reader of the file was shared. I got some problem later, and I thought of trying have

C++ const lvalue references

偶尔善良 提交于 2019-12-23 07:59:14
问题 Assuming I have: class A which is non-copyable class B which has as a member, const A& a (and takes an A in its constructer and sets it in its initialization list) a function A GenerateA(); Does this mean that it should be valid to do: B(GenerateA()) ? i.e, does the const ref mean that no copy of the A that generateA() returns is done? And does that mean that the scope of the returned temporary is extended for as long as B exists? EDIT: Addon question from the comments: Is it acceptable to

Implementation supplied copy constructor and assignment operator

拟墨画扇 提交于 2019-12-22 11:26:41
问题 I have a small confusion regarding the situations where the implementation (compiler) will not supply the copy constructor and the copy assignment operator. When we declare the copy ctor and/or copy assignment operator in our class. Some says when we derive from a class which has a private copy ctor and/or copy assignment operator. I am a little confused about the second situation, is the second situation is precisely. a) The implementation will not declare them for you, so you will get a

How to store persistent handles in V8?

久未见 提交于 2019-12-19 02:05:18
问题 I want my class to hold a v8::Context and a v8::External as members. Therefore, I thought I had to use persistent handles. class ScriptHelper { public: ScriptHelper(v8::Persistent<v8::Context> Context) : context(Context) { // ... } // ... private: v8::Persistent<v8::Context> context; v8::Persistent<v8::External> external; }; However, persistent handles are non copyable in V8, so the code does not compile. The error occurs in the lines where the two memberes get initialized. For the context,

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

对着背影说爱祢 提交于 2019-12-17 07:09:40
问题 See title. I have: class Foo { private: Foo(); public: static Foo* create(); } What need I do from here to make Foo un-copyable? Thanks! 回答1: 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 {

Inserting an object having a non copyable field into an std::vector

我与影子孤独终老i 提交于 2019-12-13 00:24:25
问题 I understand that the following code does not compile since the move constructor of A is deleted because the mutex is not movable. class A { public: A(int i) {} private: std::mutex m; }; int main() { std::vector<A> v; v.emplace_back(2); } But if I want my A to be stored in an std container how should I go about this? I am fine with A being constructed "inside" the container. 回答1: std::vector::emplace_back may need to grow a vector's capacity. Since all elements of a vector are contiguous,