I\'ve not been able to find too much information about them online. What are they and when are they typically used?
Thanks.
Here is a brief description that is valid for lists as well:
Object to be stored contains additional information to allow integration in container. Example:
struct Node { Node* next; // additional Node* prev; // information T data; }1. Pros:
- stores the objects themselves.
- doesn't involve memory management.
- iteration is faster.
- better exception guarantees.
- predictability in insertion and deletion of objects. (no additional (non-predictable) memory management is required.)
- better memory locality.
2. Cons:
- contains additional data for container integration. (every store type must be adapted (modified) to the container requirements.)
- caution with possible side effects when changing the contents of the stored object.(especially for associative containers.)
- lifetime management of the inserted object, independently from the container.
- object can be possibly disposed before erased from the container leading to iterator invalidation.
- intrusive containers are NON-copyable and NON-assignable.
Object doesn't "know" and contain details about the container in which is to be stored. Example:
struct Node { T data; }1. Pros:
- does not containe additional information regarding the container integration.
- object's lifetime managed by the container. (less complex.)
2. Cons:
- store copies of values passed by the user. (inplace construction possible.)
- an object can belong only to one container. (or the contaier should store pointers to objects.)
- overhead on storing copies. (bookkeeping on each allocation.)
non-copyable or non-movable objects CAN'T be stored in non-intrusive containers.- can't store derived object and still maintain its original type. (slicing - looses polymorphism.)