2

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?

Touloudou
  • 2,079
  • 1
  • 17
  • 28
  • Did you try it ? – Jabberwocky Feb 07 '22 at 12:58
  • You're not using the `T` template parameter... Anyway, show a [mcve] – Jabberwocky Feb 07 '22 at 12:59
  • Does this answer your question? [How to get unique values at preprocessing across files](https://stackoverflow.com/questions/25204389/how-to-get-unique-values-at-preprocessing-across-files) – Lanting Feb 07 '22 at 13:02
  • This code is not thread safe. After the "first" call to the function, two threads could enter, both see the variable is initialized to some value `N`, and then both functions will return `N` and who knows what happens to the static counter after that. – NathanOliver Feb 07 '22 at 13:08
  • Thread safety is not an issue here. – Touloudou Feb 07 '22 at 13:13
  • @molbdnilo I considered doing that and calling it a day, but is the code I posted correct as it is? – Touloudou Feb 07 '22 at 13:14
  • 1
    @Lanting I try to avoid the preprocessor as much as possible. – Touloudou Feb 07 '22 at 13:16
  • Will static local variables be unique in inline functions? – Sebastian Feb 07 '22 at 13:26
  • They `generate_uniuqe_id` function in each of the translation units will coalesce into a single representation (i.e., there will only be one function address for the function). q.v. https://en.cppreference.com/w/cpp/language/inline – Eljay Feb 07 '22 at 13:38
  • @eljay Thx specifically: "Function-local static objects in all function definitions are shared across all translation units (they all refer to the same object defined in one translation unit)." at your linked page. – Sebastian Feb 07 '22 at 13:43
  • @Eljay so I take it the code sample is correct then? – Touloudou Feb 07 '22 at 13:51
  • 1
    Yes, the code sample will work as you intend, with no undesirable side-effects. I'm not aware of any non-compliant compilers on this matter, but... *knock on wood*. – Eljay Feb 07 '22 at 16:28
  • @Eljay Actually, it does not quite work. In my code base, I have multiple dlls, and it seems that each dll gets its own counter. I think the only safe way forward is to define the function in one cpp, rather than have the definition in the header. – Touloudou Feb 11 '22 at 13:26
  • Ahh, I suspect DLLs do not support weak linkage. If so, an unfortunate limitation. Your solution sounds like the best way out of that pickle. – Eljay Feb 11 '22 at 13:46
  • Does this answer your question? [Sharing a global/static variable between a process and DLL](https://stackoverflow.com/questions/4911994/sharing-a-global-static-variable-between-a-process-and-dll) – Sebastian Feb 11 '22 at 14:11

0 Answers0