Why is std::is_constructible not true for a trivial aggregate type?
Asked Answered
C

2

7

In regards to C++17; GCC, Clang, and MSVC consider a trival class type not to be constructible by any of its data member types. Since C++20, GCC and MSVC changed this, allowing the example below to compile.

#include <type_traits>

struct t {
    int a;
};

static_assert(std::is_constructible<t, int>{});

Unfortunately, Clang seems to disagree and rejects this code when compiling with -std=c++20 as well. Is this a compiler bug of Clang? And why do all compilers not consider a type like t to be constructible with an int when compiling with -std=c++17? After all, t{0} seems to be pretty constructible that way.

Collis answered 11/6, 2022 at 16:2 Comment(2)
"Constructible" checks ( ), not { }, and Clang doesn't implement C++20 ( ) initialization for aggregates yet.Cavicorn
Related #69550939Translocate
C
4

Constructibility is based on the ability to use constructor syntax (T(values)). In C++20, aggregates can be initialized using constructor syntax, but in C++17 and before, they must use {} syntax.

Clang's C++20 implementation is simply not up to the standard yet.

Conquest answered 11/6, 2022 at 16:22 Comment(0)
L
-1

Clang will compile this if you add a constructor that can construct a t from an int. I'm not sure why this would not work without it in clang.

Lely answered 11/6, 2022 at 16:10 Comment(1)
"I'm not sure why this would not work without it in clang." -- considering that the question is why this does not work in clang, this answer does not seem to answer the question. You have addressed how to make it work in clang, but the question is "why".Edea

© 2022 - 2024 — McMap. All rights reserved.