What the question says. In addition, is it possible to do this inline?
Here is a small example just to give an idea...
template<typename T>
class Foo {
public:
Foo() :z(0.0) {}
void do( const Foo<T> &f ) {
z = f.z;
}
// specialize 'do' for Foo<int>, possible inline?
private:
T z;
};
You can sort of get this behavior by making the member function a member function template and using SFINAE (substitution failure is not an error). For example:
The
is_integral
type trait test whetherU
is an integer type. If it is not, the first is instantiated; if it is, the second is instantiated.The
is_same
type trait tests to ensureT
andU
are the same type. This is used to ensure that the member function template is not instantiated for any type other thanFoo<T>
.This example makes use of the C++0x
<type_traits>
library; Boost also has a type traits library that you can use, which works mostly the same.