What is the use of "= delete" in the declaration of a C ++ constructor?

14

I came across a declared constructor as follows:

State(const State& em) = delete;

Does anyone know what = delete is at the end of the signature of the counter?

    
asked by anonymous 22.03.2018 / 19:18

1 answer

14

By default C ++ creates the copy and assignment constructors ( move ) for you with a default code. If you want the class to not have these constructors you need to tell the compiler that it should be "deleted", so any attempt to call these constructors will fail.

Contrary to what it sounds like you're not creating a builder, you're forbidding it.

This exists since C ++ 11. If they had thought better it would probably be opt-in and not opt-out as it had to stay for compatibility issues. At first nobody thought there could be a class without this constructor.

It was interesting to ban the copy builder and leave only the working assignment that is much more efficient, where it fits.

If some place had the copy builder implemented and had it done like that, there's something weird about it. This is the opposite of implementing.

    
22.03.2018 / 19:25