Static counter shared by all translation units

Viewed 70

I have a class template like so

template <typename T>
struct Foo
{
  Foo() : id_{generate_unique_id()} {}
  int id_;
};

Each instance of Foo must have a unique id_. I thought I could just use a static counter like so:

namespace details { // all this code is in a header file
inline int generate_unique_id() {
    static int counter = 0;
    return counter++;
}
}

Is this code correct? Am I guaranteed that all translation units will use the same static variable counter?

0 Answers
Related