Is it possible to disable the copy constructor std :: vector?

I am writing code with a lot of STL vectors. I think I structured it so that all references and movements of constructors, but I would like to have an automated way to be sure. Is there a way to get a warning or error when calling the copy constructor?

I do not want to write my own vector class or modify the STL headers. Please do not mark this duplicate of similar questions in people who write their own classes: I do not want to do this.

+7
c ++ c ++ 11 stl
source share
3 answers

In addition to disabling the copy constructor and copy assignment operator of the type stored inside the vector, you cannot disable vector copies without changing the source code.

However, you have several options.

You can check if the vector copy constructor is present in your binary format; the optimizer should eliminate it if it is never used.

You can process the copy constructor of the type contained within the vector and see how often it is called.

You can put a breakpoint in the copy constructor (or one of the helper functions that it calls, and check the call stack on hit to see if it called the copy constructor).

Or you can temporarily wrap a vector with your own class and remove its copy constructor.

+8
source share

instead of writing your own vector class, you can create a class that comes from vector and implements all the constructors except the copy constructor. then you would #define vector my_vector . obviously, this needs to be done only to search for constructor calls, and then this code should be commented out. it should be only 50-100 lines instead of 1k lines for your own vector class.

+2
source share

You can use std::unique_ptr<std::vector> , although for indirectness there will be a bit of overhead, but a lot . must be changed to -> . This will help you prevent copying without any hacks.

+2
source share

All Articles