VSCode and GCC and Clang trivially copyable in C++20
01:50 21 Mar 2023

VSCode intellisence appears to violate C++20 standard.

Note: I'm not good at writing in English, so I use translation.

Hello, nice to meet you.

I ran into a problem while writing the following test to verify triviality in C++20. Let's take a quick look at the following definitions, for example:


template
concept copy_constructible = std::is_copy_constructible_v ;

template
concept trivially_copy_constructible = std::is_trivially_copy_constructible_v
    && copy_constructible ;


template
struct OptionalTest
{
private:
    
    union { T _value ; alignas(T) std::byte _align[sizeof(T)] ; } ;
    bool _valid ;

public:
    
    // #1
    constexpr OptionalTest(const OptionalTest &)
    requires trivially_copy_constructible
    = default ;
    
    // #2
    constexpr OptionalTest(const OptionalTest &other)
    requires copy_constructible
    : _align { /* default */ }
    , _valid(other._valid)
    {
        if (_valid) {
            new (&_value) T { other._value } ;
        }
    }

} ;

// all true.
static_assert(std::is_trivially_copy_constructible_v>) ;
static_assert(std::is_trivially_move_constructible_v>) ;
static_assert(std::is_trivially_copy_assignable_v>) ;
static_assert(std::is_trivially_move_assignable_v>) ;
static_assert(std::is_trivially_destructible_v>) ;

// this is false.
static_assert(std::is_trivially_copyable_v>) ;

As you can see in the comments in the code above, VSCode says OptionalTest is not trivial copyable. But to me OptionalTest looks trivially copyable.

Removing the definition of #2 fixes the problem, but this appears to violate the trivial copyable constraint in C++20.

In fact, even with the definition of #2, OptionalTest continues to be trivial copy-constructible as seen in the comments.

Additionally, although this issue appears in VSCode, the gcc compiler I'm using (-std=c++20 option) works as expected, making OptionalTest trivially copyable. clang treats it as false, just like VSCode.

Why? Is this my mistake? If it's my compiler, should I change the code for VSCode or clang? I want to check whether OptionalTest in this definition is trivially copyable by the C++20 standard.

thank you.

Sidenote: The thing I most want to check is whether types like OptionalTest are trivially copyable. Other information can be inferred incidentally by its truth.

c++ visual-studio-code c++20 intellisense