skydoor
skydoor

Reputation: 25898

Is there any c++ class that can't be used in STL?

Just come up with this question. Any hint?

Upvotes: 5

Views: 280

Answers (5)

Ramon Zarazua B.
Ramon Zarazua B.

Reputation: 7415

Depends on the container, for more information, section 23 of the Standard specifies the requirements for all containers and methods.

To be safe though, you should assume that the following are always required: Default Construction and Copy construction

Upvotes: 0

smerlin
smerlin

Reputation: 6566

The class may not throw exceptions in the destructor ...well no class ever should throw in the destructor

Upvotes: 2

Michael Anderson
Michael Anderson

Reputation: 73580

My favourite thing not to put into a STL container is an std::auto_ptr... very bad things happen. .. mostly unexpected loss of objects I think.

In general anything that is not copyable can't go into a container - you'll get compile errors. Something with abnormal copy semantics (like auto_ptr) should not go in a container (but you probably wont get any compiler errors). As the container is free to create various temporary copies.

I think that without a "sane" default constructor and assignment operator you are also in for some pain.

Upvotes: 9

Dennis Zickefoose
Dennis Zickefoose

Reputation: 10979

Depending on the operations you perform, you oftentimes need a default constructor in addition to being copyable for objects stored in containers. For objects passed to algoriths, there are other requirements, like being callable or incrementable. The requirements are well documented.

Upvotes: 4

Chris H
Chris H

Reputation: 6601

classes that can't be copied. STL containers require objects to be copyable since the container owns a copy of that object, and needs to be able to move it around.

Upvotes: 13

Related Questions