H
Hallo,
was ist mit der naheliegensten Variante?
template <class T>
class Foo {
public:
int f();
};
template <class T>
int Foo<T>::f() {
// Default-Impl
}
template <>
int Foo<int>::f() {
// Spezialisierung von f für Foo<int>
}
template <>
int Foo<double>::f() {
// Spezialisierung von f für Foo<double>
}
Das geht natürlich nur, wenn die Spezialisierungen alle vollständig sind.
Falls du partielle Spezialisierungen brauchst, würde ich mich zuerst fragen, ob das Design wirklich so sinnvoll ist. Vielleicht gehört die Funktion besser nicht in das Template? Lohnt es sich wirklich, dass K ein Template ist?
Falls du dann immer noch partielle Spezialisierungen deiner Funktion haben willst, hilft eine Indirektion. Z.B. so:
template <class T, class U>
class Foo {
public:
int f();
int f_1(); // Allgemeine Impl
int f_2(); // Impl für Foo<T*, U>
int f_3(); // Impl für Foo<T, U*>
};
template <class T, class U>
struct Selector {
static int f( Foo<T, U>* foo ) {
return foo->f_1();
}
};
template <class T, class U>
int Foo<T, U>::f() {
return Selector<T, U>::f(this);
}
template <class T, class U>
struct Selector<T*, U> {
static int f( Foo<T*, U>* foo ) {
return foo->f_2();
}
};
template <class T, class U>
struct Selector<T, U*> {
static int f( Foo<T, U*>* foo ) {
return foo->f_3();
}
};