Saturday, November 20, 2010

Dependent Names

Given the following:

template <typename T>
  class Bag
  {
    vector<T> d_bag;
   public: 
    int count();
    void add(T element);
    void remove(T element);
  };

The following are dependent names (wrt templates):
    a. vector<T>
    b. Bag
    c. Bag<T>
    d. this->d_bag

The following are not dependent names:
    x. count
    y. d_bag
    z. Bag<int>

'a' because the name "vecter<T>" depends on T. If T is int, then the name becomes vector<int>.
'b' because Bag's name also depends on T. If T is int, then the name becomes Bag<int>.
'c' for same reason as 'b'.
'd' because 'this' represents Bag which is a dependent name.

not 'c' because the name 'count' stays the same no matter what T is.
not 'd' for same reason as 'c'
not 'e' because the Bag<int> name no longer depends on T.

Reference: C++ Templates by David Vandevoorde and Nicolai M. Josuttis. Addison-Wesley, 2003, p. 121

No comments:

Post a Comment