partial specialzation of a member function

P

persres

Hello,
I have a template like below :

template<class T, class U> struct X
{
void f();
};

I want a partial specialization as :

template <class T>
void X<T, char>::f() { cout << "Partial specialization 1" << endl; }

It doesnt work. My Visual studio 2008 compiler says :

error C3860: template argument list following class template name must
list parameters in the order used in template parameter list
error C2976: 'X<T,U>' : too few template arguments

Any help please?
Thanks
 
F

Francesco S. Carta

on 02/09/2010 07:11:52 said:
Hello,
I have a template like below :

template<class T, class U> struct X
{
void f();
};

I want a partial specialization as :

template<class T>
void X<T, char>::f() { cout<< "Partial specialization 1"<< endl; }

It doesnt work. My Visual studio 2008 compiler says :

error C3860: template argument list following class template name must
list parameters in the order used in template parameter list
error C2976: 'X<T,U>' : too few template arguments

You cannot partially specialize only part of a struct/class - you cannot
partially specialize functions, in general.

Try something like this:

#include <iostream>

using namespace std;

template<class T, class U> struct X {
void f() {
cout << "T, U" << endl;
}
};

template<class T> struct X<T, char> {
void f() {
cout << "T, char" << endl;
}
};

int main() {
X<int, double> id;
X<int, char> ic;
id.f();
ic.f();
}
 
J

Johannes Schaub (litb)

Hello,
I have a template like below :

template<class T, class U> struct X
{
void f();
};

I want a partial specialization as :

template <class T>
void X<T, char>::f() { cout << "Partial specialization 1" << endl; }

It doesnt work. My Visual studio 2008 compiler says :

error C3860: template argument list following class template name must
list parameters in the order used in template parameter list
error C2976: 'X<T,U>' : too few template arguments

Just overload it

template<typename,typename> struct ty { };

template<class T, class U> struct X {
void f() {
f(ty<T, U>());
}

private:
template<typename Tx, typename Ux>
void f(ty<Tx, Ux>) { } // generic

template<typename Tx>
void f(ty<Tx, char>) { } // "specialized"
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top