https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110344

Anthony Lai <nylonhat at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nylonhat at gmail dot com

--- Comment #7 from Anthony Lai <nylonhat at gmail dot com> ---
(In reply to GCC Commits from comment #4)
>     c++: C++26 constexpr cast from void* [PR110344]
>     
>     P2768 allows static_cast from void* to ob* in constant evaluation if the
>     pointer does in fact point to an object of the appropriate type.
>     cxx_fold_indirect_ref already does the work of finding such an object if
> it
>     happens to be a subobject rather than the outermost object at that
> address,
>     as in constexpr-voidptr2.C.

Currently gcc allows constexpr cast from void* for non similar types such as:
    - From object to first member
    - From first member to object
    - From derived to base
    - From base to derived
    - From union to any of it's members

However it does not allow cast from union member to union. Is this intended?

union U {int member = 0;};

consteval auto test_union(){
    U u{};

    //union to any member allowed
    void* u_void_ptr = &u;
    int* int_ptr = static_cast<int*>(u_void_ptr);

    //member to union not allowed
    void* member_void_ptr = &u.member;
    U* u_ptr = static_cast<U*>(member_void_ptr);
}

See: https://godbolt.org/z/53ohd46xv

Reply via email to