Questions tagged [noncopyable]

The boost::noncopyable class is used as a base class to make a C++ type unable to be copied or assigned. This tag is for the boost library utility noncopyable.

The boost::noncopyable class is used as a base class (usually private) to make a C++ type unable to be copied or assigned.

It is intended to be used as a private base. boost::noncopyable has private (under C++03) or deleted (under C++11) copy constructor and a copy assignment operator and can't be copied or assigned; a class that derives from it inherits these properties.

Use this tag is for the boost library utility noncopyable.

76 questions
81
votes
11 answers

What are the advantages of boost::noncopyable

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?
tenfour
  • 36,141
  • 15
  • 83
  • 142
80
votes
10 answers

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

See title. I have: class Foo { private: Foo(); public: static Foo* create(); } What need I do from here to make Foo un-copyable?
anon
  • 41,035
  • 53
  • 197
  • 293
59
votes
5 answers

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

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…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
26
votes
6 answers

why does boost::noncopyable require inheritance

Adding any noncopyable member to a class would prevent the automatic generation of copy construction and assignment operator. Why does boost require inheritance to use noncopyable? I think I am not alone in my stylistic preference for class…
Mark Borgerding
  • 8,117
  • 4
  • 30
  • 51
24
votes
6 answers

c# select text from messagebox.show popup

i've been searching on google and stackoverflow for 2hours now. There has to be something i am just simply overlooking. Is there an easy way to make the text selectable in a messagebox? As of right now when i call a MessageBox.Show() i can not copy…
toosweetnitemare
  • 2,226
  • 8
  • 33
  • 44
20
votes
1 answer

Repeating Q_DISABLE_COPY in QObject derived classes

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…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
17
votes
2 answers

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

Have a look at the following code: #include #include // non-copyable but movable struct non_copyable { non_copyable() = default; non_copyable(non_copyable&&) = default; non_copyable& operator=(non_copyable&&) =…
mfontanini
  • 21,410
  • 4
  • 65
  • 73
14
votes
3 answers

Can copy constructors of containers be defined as deleted for non-copyable value types?

If we have a container with non-copyable value type, such a container class still defines the copy constructor, just it may not be invoked. using T = std::vector>; std::cout << std::is_copy_constructible_v; // prints out "1"…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
14
votes
10 answers

How to deal with noncopyable objects when inserting to containers in C++

I'm looking for the best-practice of dealing with non-copyable objects. I have a mutex class, that obviously should not be copyable. I added a private copy constructor to enforce that. That broke the code - some places simply needed to be fixed, but…
dbbd
  • 864
  • 1
  • 8
  • 23
13
votes
4 answers

Does private inheritance always mean "HAS-A"?

According to my favorite author , Mr Scott Meyers, the private inheritance and composition means the same thing aka Has-A relationship. Thus everything that can be gained from composition (containment, when class A has class B as its member) can be…
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76
12
votes
1 answer

How to initialize a container of noncopyable with initializer list?

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> vec({ std::unique_ptr(new int(0)), std::unique_ptr(new…
neuront
  • 9,312
  • 5
  • 42
  • 71
12
votes
6 answers

Choosing constructor for a noncopyable object

Assume I have a non-copyable class with multiple constructors with like this class Foo: boost::noncopyable { public: Foo(std::string s) {...}; // construct one way Foo(int i) {...}; // construct another way } Now, I want to…
Roddy
  • 66,617
  • 42
  • 165
  • 277
11
votes
3 answers

Initialize static std::map with non copyable value in a uniformed inline initialization

I'd like to initialize a static std::map where the value is not copyable. I'll call my class ValueClass. ValueClass has an std::unique_ptr as private member and I even ensure that ValueClass is not copyable by extending non_copyable that looks like…
U. Bulle
  • 1,075
  • 9
  • 20
11
votes
2 answers

Creating not copyable, but movable, objects in c++

Just a question. Looking at C++ Boost libraries (in particular boost::thread class) I ended up thinking: "how is it possible to create a class defining objects that cannot be copied but that can be returned from a function?" Well consider this…
Andry
  • 493
  • 1
  • 5
  • 6
11
votes
1 answer

init boost::optional of non-copyable object

What should I do to initialize boost::optional< T > if underlying type T is non-default constructible, non-copyable/moveable, but one's instance still can exist? Is it forbidden for boost::optional by any semantic reasons to have some member…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
1
2 3 4 5 6