In the example below, I assume there will be two different instantiations of the class template function get_count()
, which is redundant since they are not depending on the template parameter. Is this true (or optimized away?), and is there a way to make all instantiations of a template use a common function (maybe some template argument wildcard, like <*>
?) when it comes to some member functions?
template<class T>
class A {
private:
T obj;
int count;
public:
int get_count();
};
template<class T>
int A<T>::get_count() { // This function doesn't really need to
// depend on the template parameter.
return this->count;
}
int main() {
A<int> a; //<--First template instantiation
A<bool> b; //<--Second template instantiation
int i = a.get_count(); //<--Could theoretically use the same code
int j = b.get_count(); //<--
return 0;
}
Also, what if the member variables are re-arranged?