lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Just another data point, since I recently saw it:
> The well-used GLM library (which is C++) uses type punning through a
> union just about as clear as can be:
>     https://github.com/g-truc/glm/blob/b3f87720261d623986f164b2a7f6a0a938430271/glm/detail/func_common.inl#L722
> 
> I suppose, since it's C++ (not C90), it is technically undefined
> behavior. But I have certainly worked on dozens of systems over
> decades that rely on it, so it seems like extremely reliable undefined
> behavior, as far as that goes (:  And of course, it is explicitly
> supported by GCC.

I just learned that C99 was amended about that, to make clear that this
kind of tricks with unions behave as expected [1]. It now includes
the following footnote in 6.5.2.3p3:

    82) If the member used to access the contents of a union object
    is not the same as the member last used to store a value in the
    object, the appropriate part of the object representation of the
    value is reinterpreted as an object representation in the new type
    as described in 6.2.6 (a process sometimes called "type punning").
    This might be a trap representation.

(My version of the standard does not have these amendments.)

[1] http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

-- Roberto