I assume that your problem is something like this:
template <typename T>
struct Foo {
//..
void bar() {
/* most or all code here is the same for all T,
and uses nothing that depends on T. */
};
};
And you would like the define "bar" in such a way that there will only be one bar function, not one for each instantiation of "Foo", i.e., you don't want Foo<int>::bar
to be a different function from Foo<double>::bar
.
This cannot be done, because the "bar" function is a member of the class template, and therefore, of each of its instantiations.
What you can and should do instead is to define a helper (free) function that has all the "same for all T" code in it. Something like this:
namespace {
void Foo_bar_helper(/*..*/) {
/* common code goes here. */
};
};
template <typename T>
struct Foo {
//..
void bar() {
Foo_bar_helper(/* pass the "guts" of Foo<T> here. */);
};
};
Normally, the bar function will get inlined by the compiler and all that really remains is an expansion of the "guts" of the Foo object (if any) as parameters to the helper function (which you could implement with or without external linkage, as you wish).
Another option is to inherit from a non-template base class, but I personally don't like using inheritance for that kind of stuff, and you will still have to the same forwarding of the "guts" of the Foo object (if any).