views:

711

answers:

3

I find that most books concerning C++ templates don't tell anything about whether it's possible or not to use initialization list in constructor of a template class.

For example, I have code like this:

template <class T>
class Stack {
    T* data;
    std::size_t count;
    std::size_t capacity;
    enum {INIT = 5};
public:
    Stack() {
        count = 0;
        capacity = INIT;
        data = new T [INIT];
    }

Can I replace the constructor with

Stack(): count(0), capacity(INIT), data(new T [INIT])
+4  A: 

Yes. Did the compiler tell you otherwise?

Jim Buck
A: 

Of course, why would it not be?

Leon Timmermans
A: 

I've just tried and VS2008 says that it's OK, but it seems a little bit strange because some great authors don't do that (Eckel, for example, in his "Thinking in C++").

chester89