jeudi 28 septembre 2017

Nest template class method specialization

Before my question I'm posting absolute correct code that works:

template <class T>
struct Gen
{
    void method()
    {
        std::cout << "I'm generic method\n";
    }
};

template <>
void Gen<int>::method()
{
    std::cout << "I'm dedicated for int method\n";
}

int main()
{
    Gen<float> g1;
    g1.method(); //generic method

    Gen<int> g2;
    g2.method(); //int method

}

So this declares method of template class that has special behavior for int.

Now I'm trying to do absolutely the same with nested template class:

template <class T>
struct GenNest
{
    template <class R>
    struct Nest
    {
        void method()
        {
            std::cout << "I'm generic method\n";
        }
    };
};

template <class T>
template <>
void GenNest<T>::Nest<int>::method()
{
    std::cout << "I'm dedicated for int method\n";
}

int main()
{
    GenNest<float>::Nest<float> n1;
    n1.method(); //generic method
}

And this case fails on compile time

  • for vc++: error C2995: 'void GenNest::Nest::method(void)' : function template has already been defined
  • for g++ error: invalid explicit specialization before '>' token

So dear community - what is wrong and how to fix it?

Aucun commentaire:

Enregistrer un commentaire