Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Conundrum

I have encountered a C++ template conundrum. I've tried to trim it down to the bare minimum, and now I'm not even sure if what I'm trying to do is possible. Take a look at the following code (in some .h file).

template<typename T>
class A
{
public:
    template<typename S>
    void f(S x);
};

class B1 { };

template<typename S>
class B2 { };

//This one works:
template<>
template<typename S>
void A<B1>::f(S x)
{
}    

//This one does not work:
template<>
template<typename S>
void A<B2<S>>::f(S x)
{
}

In my main function I have something like this:

//This one works:
A<B1> first;
first.f<int>(5);

//This one does not work:
A<B2<int>> second;
second.f<int>(5);

The error message I get because of the second part is

error C3860: template argument list following class
             template name must list parameters in the
             order used in template parameter list

error C3855: 'A<T>': template parameter 'T' is
             incompatible with the declaration

Any idea what the problem is?


Edit

To make the problem more concrete, here's my motivation. I want the function f above to have specializations for T=std::tuple<T1, T2>, T=std::tuple<T1, T2, T3>, and T=std::tuple<T1, T2, T3, T4>, where the types in the tuple are still unbound.

like image 892
Timothy Shields Avatar asked Jan 14 '23 00:01

Timothy Shields


1 Answers

You are trying to partially specialize a member function template. That is not possible, I'm afraid...

like image 97
Paul Michalik Avatar answered Jan 23 '23 11:01

Paul Michalik