jeudi 4 mai 2017

How to specialize a template for 2 different values?

Given the code below, I was wondering if it is possible to have a specialization for a set of values. In my example, I want to create an specialization for N=3 or N=4 to use an array of known size. Is it possible to avoid code duplication in this case?

template <typename T, unsigned int N>
class A
{
public:
    T* data;
};

template <typename T>
class A<T, 3>
{
public:
    T data[3];
};

template <typename T>
class A<T, 4>
{
public:
    T data[4];
};

int main()
{
    A<int, 1> u;
    std::cout << sizeof(u.data) << std::endl; // Size of pointer

    A<int, 3> v;
    std::cout << sizeof(v.data) << std::endl; // Size of data

    A<int, 4> w;
    std::cout << sizeof(w.data) << std::endl; // Size of data

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire