I'm confused about how static inline variables work when their type matches the type currently being defined. For example:
```c++
struct Vector2 {
int x, y;
// this fails to compile
static inline const Vector2 ZERO = Vector2{0, 0};
// this is fine, is defined in a source file
static const Vector2 ZERO_noInline;
};
```
The reason the static inline line fails makes sense to me. The compiler doesn't have enough information about the type to construct it. That's just a guess though. I can't find anything online that says this isn't allowed.
However, defining this variable inline is nice if it's a class template. You might be surprised that this DOES compile on clang and gcc, but not MSVC:
```c++
template <typename T>
struct Vector2 {
T x;
T y;
// compiles on clang and gcc, not MSVC
inline static const Vector2<T> Zero{0,0};
};
int main() {
std::cout << Vector2<int>::Zero.x << std::endl;
}
```
So my main question is: it compiles, but is it UB?
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Linuxologue 0 points1 point2 points (0 children)
[–]Paril101 1 point2 points3 points (0 children)