Issue 125999
Summary Templated static member variables are initialized in the including module instead of the global module
Labels new issue
Assignees
Reporter SrivastavaAnubhav
    When I include a header that contains a templated class with static member variables (and those variables are initialized outside the class) in the global module fragment of my module class, Clang determines that the initialization is in **my** module (not the GMF). See the following example: https://gcc.godbolt.org/z/ej6K4h6ds. Also pasting below for convenience.

`bar.h`:
```
template <typename T>
struct Singleton {
    static T* instance_;
    static T* get() {
 static bool init = false;
        if (!init) {
            init = true;
            instance_ = ::new T();
        }
        return instance_;
    }
};

template <typename T>
T* Singleton<T>::instance_ = nullptr;

struct s{};
inline void* foo() {
    return Singleton<s>::get();
}
```

`foo.ixx`:
```
module;
#include "bar.h"
export module foo;
```

This fails to compile because `declaration of 'instance_' in module foo follows declaration in the global module`, but the include is in the global module fragment so shouldn't the declaration also be there (not in `Foo`)? This compiles successfully on GCC.
```
bar.h:20:18: error: declaration of 'instance_' in module foo follows declaration in the global module
   20 | T* Singleton<T>::instance_ = nullptr;
      |                  ^
bar.h:13:13: note: in instantiation of static data member 'Singleton<s>::instance_' requested here
   13 | instance_ = ::new T();
      |             ^
bar.h:25:26: note: in instantiation of member function 'Singleton<s>::get' requested here
   25 | return Singleton<s>::get();
      | ^
bar.h:8:15: note: previous declaration is here
    8 |     static T* instance_;
```


_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to