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
2
votes
2 answers

How to determine whether a c++ class is copyable

I'm interested in improving my understanding of how to avoid writing a C++ class that causes problems when copied. In particular, I've written a class named Policy that I intend to copy. I have not defined a non-default destructor, copy…
synaptik
  • 8,971
  • 16
  • 71
  • 98
1
vote
1 answer

In a boost::noncopyable class, setting the move constructor "=default" does not compile?

Consider the following code. In order for this to compile, I HAVE to provide a body for the move constructor. Having "TestClass(TestClass&& other) = default" does not compile with an error that says "TestClass::TestClass(TestClass&&)' is implicitly…
rmsantos
  • 105
  • 6
1
vote
1 answer

Move an object containing a unique_ptr to vector

Just wondering if there is a way to move an object holding a unique_ptr into a vector of those objects? Example: class A { public: std::unique_ptr ptr; }; std::vector objects; A myObject; //move myObject to objects??? Now, is…
1
vote
1 answer

Aggregate initialization of noncopyable base class

I am constructing derived class from non-copyable base class. I would like to aggregate-initialize Base in the initializer: // for convenience, could be any other way to disable copy #include struct Base: public…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
1
vote
1 answer

Build a .cu file that uses boost

I ran the following command: nvcc -arch=sm_70 foo.cu -o predatorPrey -I $BOOST_ROOT -L $BOOST_LIBRARY_PATH -lboost_timer And got the following compilation error: boost/include/boost/core/noncopyable.hpp(42): error: defaulted default constructor…
Cydouzo
  • 445
  • 5
  • 18
1
vote
1 answer

Should Non-Copyable class have user conversion

My question is, should non-copyable objects have implicit/explicit user conversion? At least from my example below, conversions look very much like copies. PS: I know that is recomended here to "Avoid implicit conversion operators"…
1
vote
1 answer

Placement new for non-copyable objects

I'm trying to simulate a vector of unique pointers just to learn how placement new works with objects that cannot be copied. class Person { string name; const int born; public: Person (string N, int B) : name(N), born(B) {} }; int…
visitor
  • 672
  • 6
  • 17
1
vote
1 answer

How to have "factory function" return a non copyable object?

Context Trying to create some gzip archive with a different filename inside I wrote this following snippet of code. #include #include #include #include…
matovitch
  • 1,264
  • 11
  • 26
1
vote
1 answer

C++: working with multiple copies of std::cin?

I am writing a simple generalised parser combinator library. This means the library contains many small function objects, called parsers, which (when called) take a string as input and return a list of ParseResults as output, where a ParseResult…
Qqwy
  • 5,214
  • 5
  • 42
  • 83
1
vote
1 answer

Emplace a derived movable but noncopyable in a vector gives compilation error

I'm having compiler errors when trying to emplace_back in a vector of non-copyable but movable objects with a subtle inheritance twist, that should to my knowledge not change the problem. Is this legal C++ and this a Visual Studio 2015 bug, or am I…
N0vember
  • 995
  • 1
  • 9
  • 12
1
vote
1 answer

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

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 v; …
user695652
  • 4,105
  • 7
  • 40
  • 58
1
vote
2 answers

How to use vector (or different container) with non copyable class?

I have the following piece of code: void f(){ ..... std::vector v; for(int i = 0; i < list.length(); ++i){ PrepareLogFileThread a( list[i], LOG_TEMPLATE); a.start(); …
Patryk
  • 22,602
  • 44
  • 128
  • 244
1
vote
1 answer

Is it possible to initialize a vector of vectors of non-copyable objects?

Is it possible to initialize a vector of vectors of non-copyable objects? class obj : private boost::noncopyable { // ... }; vector > v(10); // OK for(int i = 0; i < v.size(); ++i) v[i].resize(10); //…
MWB
  • 11,740
  • 6
  • 46
  • 91
1
vote
5 answers

Is it good practice to generally make heavyweight classes non-copyable?

I have a Shape class containing potentially many vertices, and I was contemplating making copy-constructor/copy-assignment private to prevent accidental needless copying of my heavyweight class (for example, passing by value instead of by…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
1
vote
2 answers

C++ optional<> and delayed construction of noncopyable object

See the below code, The question is: how can I delay the construction of an object that is non-copyable, using optional<>. I'm using boost::optional in the example, although I believe its now in the std::optional standard too. Yes, I could use…
elegant dice
  • 1,307
  • 12
  • 19