(C++'s STL concept:)

A C++ type is Assignable if it can be assigned and copied. Specifically, it has a copy constructor and an assignment operator (operator=) with the "usual" semantics. The standard swap function will therefore work for it, with the "usual" semantics.

In fact, the C++ templated swap function uses only the copy constructor and assignments. So if the class has appropriate copy constructor and assignment operator, it is of necessity "swappable" (and with no further effort by the programmer). However, some classes may have more efficient swap operations. For instance, any STL container has a swap method which is more efficient. If class C has such a method, it makes sense to define
template<> swap(C& a, C& b)
to get a more efficient operation. An additional requirement (since STL algorithms use swap) is that swap too have its "usual" semantics.

The model for Assignable is int, but any of the builtin data types (i.e. excluding void) will do. Pointers also qualify, if you consider what pointer assignment does to be the creation of a copy. Most classes with operator= and a copy constructor (or even the default copy constructor) will also do.

As a nonexample, any auto_ptr<T> is not Assignable (after an assignment x=y;, x is not a copy of y, since y no longer owns the block of memory).

Most STL containers demand that they hold an Assignable type. In particular, anything like vector<auto_ptr<T> > is wrong.

As*sign"a*ble (#), a.

Capable of being assigned, allotted, specified, or designated; as, an assignable note or bill; an assignable reason; an assignable quantity.


© Webster 1913.

Log in or registerto write something here or to contact authors.