Does this piece of code violate the strict rule of aliases?

I read http://blog.qt.digia.com/blog/2011/06/10/type-punning-and-strict-aliasing/ and found this piece of code.

QDataStream &QDataStream::operator>>(qint16 &i) { ... register uchar *p = (uchar *)(&i); char b[2]; if (dev->read(b, 2) == 2) { *p++ = b[1]; *p = b[0]; ... 

The author claims that MSVC optimizes tasks that I find rather strange.

Does MSVC really use a strict anti-aliasing rule?

And is not uchar * specifically authorized to use for type writing?

+5
source share
1 answer

This is either a compiler error or an error in the code that called this method. The Strict Alias ​​Rule allows object i to refer to access using character types, regardless of what type of object actually exists. The code that calls this method does not even have to pass a reference to a qint16 compatible object for this function to determine the behavior.

+1
source

Source: https://habr.com/ru/post/1200519/


All Articles