خرید بک لینک

Vote count: 1

Reading quote in this answer about strict aliasing rule, I see the following for C++11:

If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:

  • ...

  • an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),

  • ...

So I take it to mean that the following code doesn't break strict aliasing rule:

#include <iostream>
#include <cstdint>
#include <climits>
#include <limits>

struct PuerToUInt32
{
    std::uint32_t ui32;
    float fl;
};

int main()
{
    static_assert(std::numeric_limits<float>::is_iec559 &&
                  sizeof(float)==4 && CHAR_BIT==8,"Oops");
    float x;
    std::uint32_t* p_x_as_uint32=&reinterpret_cast<PuerToUInt32*>(&x)->ui32;
    *p_x_as_uint32=5;
    std::cout << x << "n";
}

So OK, strict aliasing rule is satisfied. Does this still exhibit undefined behavior for any other reason?

asked 27 secs ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 4 تير 1395 ساعت: 3:04

صفحه بندی