lundi 27 juillet 2015

partial specialization of template class method [duplicate]

This question already has an answer here:

I feel like I have tried all possible permutations of syntaxes, but I'm not able to make the following code compile, even though what I'm trying to achieve is pretty straightforward: I want the template class method to behave differently if the class is specialized in a certain way. I think this snippet is self-explanatory:

#include <iostream>

template<class F, class T = void>
struct A {
   void foo();
};

template<class F, class T>
void A<F,T>::foo()
{
   std::cout << "two\n";
}

template<class F>
void A<F,void>::foo()
{
   std::cout << "one\n";
}

int main() {
   int tmp = 0;
   A<double, double> a;
   a.foo();
   A<int> b;
   b.foo();

   return 0;
}

I have perused the numerous questions about template specialization on stackoverflow, and that helped me get this far. At this point I feel like this should compile, and have no idea what the right syntax could be.

Aucun commentaire:

Enregistrer un commentaire