Consider the following C++ code example:
#include<iostream>
class B {
public:
int val;
B(int v):val(v){
std::cout<< val << "n";
}
};
template<typename T>
class A {
public:
static B b;
};
template<T>
typename B A<T>::b = B(1);
int main() {
A<int>::b.val;
return 0;
}
On GCC 11.4.0, build with g++ main.cc -g
got output 1
.
On Clang 14.0.0, build with clang++ main.cc -g
got segfault
.
Ubuntu 22.04.4 LTS
Cannot understand the reason of such behavior, would be very grateful for any help.