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_integraltype trait test whetherUis an integer type. If it is not, the first is instantiated; if it is, the second is instantiated.The
is_sametype trait tests to ensureTandUare 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.