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

Louis Dionne <ldionne.2 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ldionne.2 at gmail dot com

--- Comment #3 from Louis Dionne <ldionne.2 at gmail dot com> ---
I concur with the reporter of this issue. `bit_cast` requires the types to be
TriviallyCopyable. In turn, TriviallyCopyable requires (for class types) that
there's at least one eligible copy constructor, move constructor, copy
assignment operator, or move assignment operator. It doesn't say which of those
has to be valid, but at least one of those has to be valid.

However, GCC's implementation of __builtin_bit_cast seems to always require at
least a copy or a move constructor, which seems like a bug to me.

For example, the following code should be valid IIUC, but it fails with GCC:

  struct CopyAssignable {
    CopyAssignable() = default;
    int value = 0;

    CopyAssignable(const CopyAssignable&)            = delete;
    CopyAssignable(CopyAssignable&&)                 = delete;
    CopyAssignable& operator=(const CopyAssignable&) = default;
    CopyAssignable& operator=(CopyAssignable&&)      = delete;
  };

  struct MoveAssignable {
    MoveAssignable() = default;
    int value = 0;

    MoveAssignable(const MoveAssignable&)            = delete;
    MoveAssignable(MoveAssignable&&)                 = delete;
    MoveAssignable& operator=(const MoveAssignable&) = delete;
    MoveAssignable& operator=(MoveAssignable&&)      = default;
  };

  int main(int, char**) {
    CopyAssignable foo1;
    (void)__builtin_bit_cast(CopyAssignable, foo1); // doesn't work
    MoveAssignable foo2;
    (void)__builtin_bit_cast(MoveAssignable, foo2); // doesn't work
  }

Full example on Godbolt showing that this is accepted by Clang and MSVC but
rejected by GCC: https://godbolt.org/z/548YKndrK
I am running into this issue while trying to fix something in libc++ here:
https://reviews.llvm.org/D154613

Reply via email to