C++'s Strict Aliasing Rule - Is the 'char' aliasing exemption a 2-way street?

后端 未结 2 684
别那么骄傲
别那么骄傲 2020-12-29 09:08

Just a couple weeks ago, I learned that the C++ Standard had a strict aliasing rule. Basically, I had asked a question about shifting bits -- rather than shifting each byte

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 09:28

    As a matter of fact, explanation of standard rule regarding pointer type punning through strict aliasing is not neccessarily correct or easy to understand. Standard doesn't mention 'strict aliasing', and I find original standard wording easier to understand and reason about.

    In essence, it says that you can only access an object thorugh a pointer to the related type which is suited to access this object (such as the same type or related class type) or through a pointer to char.

    As you see, the question of 'two-way street' is not even applicable.

提交回复
热议问题