roschach
roschach

Reputation: 9366

What are in C++ types that cannot be copied?

In the book "C++ Programming language" by Bjarne Stroustrup when talking about exceptions it says the following:

An exception is object thrown to represent the occurrance of an error. It can be of any type that can be copied but it is strongly recommended to use only user-defined types specifically defined for that purpose.

Reasoning about it I cannot immediately think of objects that cannot be copied. What are types that cannot be copied in C++?

Upvotes: 0

Views: 500

Answers (1)

Jack
Jack

Reputation: 133609

To be copyable an object must define at least one of two possible ways of being copied:

  • copy assignment T& operator=(const T&)
  • copy constructor T(const T&)

If no one of these is defined or it has been explicitly deleted (= delete) then you can't copy the object.

The requirement arises from the fact that exception handling must be able to copy the exception object itself somewhere for proper management.

Upvotes: 5

Related Questions