Disabling the default constructor for classes other than POD

As I understand it, the default instance constructor of C ++ only works as expected when the class is a POD class.

I would like to know if there is a solution preventing the programmer from writing code that (implicitly or not) uses the default copy constructor if the object is not a POD.

I know that you can always make your copy and appointment private to solve this problem, but I would like to know if there is an automatic solution. For example, can the compiler generate a warning if your code generates a default constructor call and your class is not a POD?

The goal is to detect cases when I forgot to declare copy / assignement private or manually define them.

Also do you know if cppcheck can do this?

+3
source share
4 answers

A centralized way to disable the default constructor is to make the default constructor unavailable.

You write: "I would like to know if there is a solution preventing the programmer from writing code that (implicitly or not) uses the default copy constructor if the object is not a POD."

Presumably, you mean that the compiler should respond to any default construct of any object other than POD.

Sorry, does not depend on the compiler.

: -POD-, , std::vector, , .

g++ -Weffc++, Scott Meyers & rsquo; ++, , , ndash; . . , -, .

hth.,

+2

++ 0x - :

struct NonCopyable {
    NonCopyable & operator=(const NonCopyable&) = delete;
    NonCopyable(const NonCopyable&) = delete;
    NonCopyable() = default;
};

. . , , , .

+3

, .

+2

When creating a class, you have 3 options: have a default copy of ctor, write your own or disable it (in various ways, do this; inherit from boost :: notcopyable to mention this). It is not entirely clear why the compiler should warn you about choosing one of them.

+2
source

Source: https://habr.com/ru/post/1776431/


All Articles