The following code gives me warning in gcc that I break strict aliasing rules:
struct Base {
int field = 2;
};
template <typename T>
struct Specialization: public Base {
void method() {
Specialization copy;
field = copy.field;
}
};
int main() {
Specialization<int> s;
s.method();
}
warning: dereferencing type-punned pointer will >break strict-aliasing rules [-Wstrict-aliasing] field = copy.field;
When I remove the template, is seems to compile just fine.
struct Base {
int field = 2;
};
struct Specialization: public Base {
void method() {
Specialization copy;
field = copy.field;
}
};
int main(){
Specialization s;
s.method();
}
Am I really breaking strict aliasing rules or is it GCC producing a false positive?
I'm using -Wstrict-aliasing=3 -O3
on GCC8