Just another WordPress.com site

Container assignment lists

A few weeks ago while reading the Blitz++ Documentation I noticed Blitz++ was initializing Matrices and other containers using commas.

  1. Container<int> bucket;
  2. bucket = 1, 2, 3, 4, 5;

Finding this both useful and cool, had to figure out how it was done.

But first…

If the container has a std::vector member then a constructor taking both the start and end of the array can be used to initialize vector. First an array is initialized, and the beginning and end of the array is passed to the container’s constructor.

  1. const int elements = 5;
  2. int a[elements] = {1, 2, 3, 4, 5, };
  3. Container<int> vec(a, &a[elements]);

Although this is doable it isn’t flexible and you might be incline to code a bunch of push_back calls

  1. Container<int> vec;
  2. vec.push_back(1);
  3. vec.push_back(2);
  4. vec.push_back(3);
  5. vec.push_back(4);
  6. vec.push_back(5);

If we have to go that route it would be much nicer to write code as shown at the beginning of the article.

Overloading a couple operators

The goal again, is to be able to write code such as

  1. Container<int> bucket;
  2. bucket = 1, 2, 3, 4, 5;

which assigns values to a container using a list.

Below is code for a template container class that has std::vector as a member. Only the two operators to be overloaded are shown in the example.

Container class
  1. template<class T>
  2. class Container
  3. {
  4. public:
  5. std::vector<T> & operator=(const T & nVal) {
  6. m_vector.clear();
  7. m_vector.push_back(nVal);
  8. return m_vector;
  9. }
  10. friend std::vector<T> & operator,(std::vector<T> & lst, T val)
  11. {
  12. lst.push_back(val);
  13. return lst;
  14. }
  15. std::vector<T> m_vector;
  16. };

Assignment operator

The assignment operator is overloaded to take a const reference T as the first parameter and returns a reference to m_vector. In the previous examples T is an int.  When the compiler sees

  1. bucket = 1

it grabs the value to the right of = and if the correct type, produces code for the assignment operator. The overloaded operator= first clears the contents of m_vector, then pushes the value onto m_vector.

The assignment operator returns a reference of m_vector which allows the overloaded comma operator to be called from the assignment list.

Comma operator

The second operator overloaded for the container class is the comma operator, which evaluates the object type to the left, evaluates the object type to the right and operates on it.

For the purposes of the example, the assignment operator returned a reference to std::vector<int> (m_vector) from the first item in the list.

  1. bucket = 1, 2, 3, 4, 5;

The compiler uses the returned type on the first seen comma (LHS), looks to the right of the comma and sees it is an int type (RHS). This matches the overloaded comma operator’s definition of

  1. friend std::vector<int> & operator,(std::vector<int> & lst, int val)

Since the comma operator is a binary operator, it needs have global scope and declaring the function as a friend provides that.

For the container class the overloaded comma operator simply pushes the RHS hand value onto the LHS object, and returns a reference to the std::vector<T> (m_vector), and repeats until no items in the list are left.


This type of operator overloading can eliminate ugly lists of repeated push_back calls and can easily be used with the other STL containers.

It is important that your container class has a member of the STL container you want to use. Do not inherit from one of the STL containers. They do not have a virtual destructor, which is needed for a derived class.


July 9, 2010 Posted by | C++ Programming, STL Containers, Templates | , , , , , | 1 Comment