tags:

views:

63

answers:

2

I have a template class for thread-safe vector:

template <class T>
class SharedVector {
    std::vector<T> vect;
    CRITICAL_SECTION cs;
    SharedVector(const SharedVector<T>& rhs) {}
public:
    typedef typename std::vector<T>::size_type SizeType;
    SharedVector();
    void PushBack(const T& value);
    void PopBack();
    SizeType size();
    const T& operator[](int index);
    void erase(int index);
    void Lock(); 
    void Unlock(); 
    virtual ~SharedVector();
};

Then I want to use it in my client manager for TCP server to delegate some responsibilities from client manager to this vector:

class TCPClientManager {
    TCPClientManager(const TCPClientManager&) {}
    TCPClientManager& operator=(const TCPClientManager&) {}
    SharedVector<Connection*> connections;
public:
    TCPClientManager();
    SharedVector<>::SizeType size(); //here is the problem
    void addConnection(const Client&); 
    void breakConnection(int); 
    void deleteConnection(int); 
    void Lock();
    void Unlock();
    ~TCPClientManager();
};

typename SharedVector<>::SizeType TCPClientManager::size() {
    return connections.size();
}

I need to declare the type of retrieving value. Compiler said that it was too few arguments for template.

+1  A: 

You have to provide a type argument for the SharedVector template:

SharedVector<Connection*>::SizeType size(); 
....
SharedVector<Connection*>::SizeType TCPClientManager::size() {
    return connections.size();
}

Because that Connection* type is not a template parameter in TCPClientManager, but an explicit chosen type, you don't need to put typename before SharedVector<Connection*>::SizeType

Johannes Schaub - litb
A: 

(In addition to litb, really)

You should typedef your container; i.e. typedef SharedVector<Connection*> ConnectionPool;. This would allow you to write ConnectionPool::size_type.

Note: container::size() should return a container::size_type, not container::SizeType. This makes the container STL-compatible. For the same reason, the iterator class should be container::iterator, adding elements is done by container::push_back(container::value_type const&), etc.

MSalters