Member function templates and static members

Can I declare a static member variable inside a member function template? How would I define it?

Yes, you can declare a static member variable inside a member function template. However, there are a few important things to note about how these interact.

First, each instantiation of the member function template gets its own copy of the static variable. This means that if you instantiate the member function template with different types, each instantiation will have a separate static variable.

Here's an example:

#include <iostream>

class MyClass {
 public:
  template <typename T>
  void func() {
    static int count = 0;
    count++;
    std::cout << "func<" << typeid(T).name()
      << ">: " << count << std::endl;
  }
};

int main() {
  MyClass obj;
  obj.func<int>();     // prints: func<int>: 1
  obj.func<int>();     // prints: func<int>: 2
  obj.func<double>();  // prints: func<double>: 1
  obj.func<int>();     // prints: func<int>: 3
}
func<int>: 1
func<int>: 2
func<double>: 1
func<int>: 3

In this code, MyClass::func declares a static variable count. However, because func is a member function template, each instantiation of func gets its own count. So func<int> has one count, and func<double> has a separate count.

It's also worth noting that if you want to define the static variable outside the class (which is necessary if it's a class type or an array), you need to provide the full template declaration:

template<>
int MyClass::func<int>::count = 0;

This can get quite complex, especially if the class itself is also a template.

In general, it's often simpler and clearer to avoid static variables inside member function templates unless you specifically need a separate static variable for each instantiation. If you do need per-instantiation static data, consider making the entire class a template instead, and putting the static variable at the class level.

Member Function Templates

Learn how to create and use member function templates in classes and structs, including syntax, instantiation, and advanced techniques

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

When should I use member function templates vs regular member functions?
How do I decide when it makes sense to use a member function template instead of a regular member function?
Passing member function templates as arguments
Can I pass a member function template as an argument to another function? How would I do that?
Template specialization for member function templates
Is it possible to specialize a member function template? How would I do that?
Using member function templates with inheritance
How do member function templates interact with inheritance? Can I override a member function template in a derived class?
Using SFINAE with member function templates
Can I use SFINAE (Substitution Failure Is Not An Error) with member function templates? How does that work?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant