Can literal type in C++ contain the only copy constructor having additional parameters with default values?

Viewed 171

The definition of copy constructor from https://en.cppreference.com/w/cpp/language/copy_constructor is

A copy constructor of class T is a non-template constructor whose first parameter is T&‍, const T&‍, volatile T&‍, or const volatile T&‍, and either there are no other parameters, or the rest of the parameters all have default values.

And the definition of LiteralType from https://en.cppreference.com/w/cpp/named_req/LiteralType includes

... a type with at least one constexpr (possibly template) constructor that is not a copy or move constructor ...

Does it mean that a class containing only one copy constructor having additional parameters all with default values is not a literal type? That would be strange since such constructor is not only a copy constructor.

For example:

struct A {
    constexpr A(const A &) {}
    constexpr A(const A &, int) {}
};
constexpr A a{ a, 1 }; //ok everyhere

struct B {
    constexpr B(const B &, int = 0) {}
};
constexpr B b{ b, 1 }; //GCC and Clang error

GCC and Clang prints the error in the last line:

error: constexpr variable cannot have non-literal type 'const B'
note: 'B' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors

while MSVC accepts the code. Demo: https://gcc.godbolt.org/z/71YfhfGGG

Which compiler is right here?

0 Answers
Related