samedi 2 janvier 2016

Gaining access to class members from template specialization

So, I basically want to "add" extra stuff to the class, depending on what template arguments its being used with and then aliasing it for nicer interface. Something like this:

template<typename T, std::size_t N>
struct basic_foo
{
    T data[N];
};

//what you see as comments is what i tried
template<> //template<typename T, std::size_t N>
struct basic_foo<double, 3> //: public basic_foo<T, N>
{
    void foo_fun() 
    {
        std::cout << "I'm a foo function!"; 
        for (auto i : data) std::cout << i << " ";
    }
};

template<> //template<typename T, std::size_t N>
struct basic_foo<int, 2> //: public basic_foo<T, N>
{
    void bar_fun() 
    {
        std::cout << "I'm a bar function!";
        for (auto i : data) std::cout << i << " ";
    }
};

using foo = basic_foo<double, 3>;
using bar = basic_foo<int, 2>;

int main()
{   
    foo a = { 12.0, 2.4, 3.0 };
    bar b = { 1, 2 };
}

The problem is that I can't access data in the specializations.

Is there a way to do this? Or should I rethink my structural decisions?

Aucun commentaire:

Enregistrer un commentaire