<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/125999>125999</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Templated static member variables are initialized in the including module instead of the global module
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
SrivastavaAnubhav
</td>
</tr>
</table>
<pre>
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_;
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0Vt-PmzgQ_mucl1GRMUuABx5otqlOp3vale7xZGAAV8bO2Sa76V9_Mr9C0rRVTyqKIBhm5vtm5hvDrRWtQsxJ_JHEzzs-uE6b_MWIM7eOn3mhhrLj512p60v-d4cK_gChKjnUCBw65DUacB13UGnluFAWODjsT5I7rKGS3Fp4E64D67gTFfTYl2j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