mercredi 20 juillet 2016

Template argument for non-type parameter must be an expression

Why is the following code wrong?

template <typename T, int N>
struct Vector {
    T data[N];
};

struct Dynamic {
};

template <typename T>
struct Vector<T, Dynamic> {
    T* data;
};

I tried to instantiate the code with Vector<int, Dynamic> obj; and it didn't work

error: template argument for non-type template parameter must be an expression

Why? I'm passing a type and I thought that was a valid template specialization.

How can I use two templates and having one that requests N and one that doesn't if marked with Dynamic ?

Aucun commentaire:

Enregistrer un commentaire