Strict alias violation

Is the following program a violation of the strict anti-aliasing rule?

#include <cstdint> int main() { double d = 0.1; //std::int64_t n = *reinterpret_cast<std::int64_t*>(&d); // aliasing violation //auto n{*reinterpret_cast<std::int64_t*>(&d)}; // aliasing violation auto nptr{reinterpret_cast<std::int64_t*>(&d)}; auto& n{*nptr}; ++n; } 

There are no warnings issued by VS2015, clang or gcc .

+6
source share
2 answers

Is the following program a violation of the strict anti-aliasing rule?

Yes Yes. You are looking for a double* ( &d ) with std::int64_t* .

A string that violates the strict aliases rule:

 auto& n{*nptr}; 

When processing a string, compilers do not necessarily know how you set the nptr value. The fact that it is an alias of double* is not obvious when processing this string.

+5
source

Yes, this violates a strict alias. You are accessing an object d type double , although the pointer is nptr , which is not a pointer to double or any type associated with it.

Just because the compiler does not generate a warning does not mean that it is not a violation. Severe violations are UB (because they are a matter of behavior during operation) and therefore do not require diagnostics.

+5
source

All Articles