Question: Why is_default_constructible<Class>::value fails within the same class scope


Why is_default_constructible<Class>::value fails within the same class scope

Answers 1
Added at 2016-12-27 10:12

Following works fine:

struct X { };  // OK
static_assert(std::is_default_constructible<X>::value, "Error"); 

Following assert fails to compile:

struct X { static_assert(std::is_default_constructible<X>::value, "Error"); };  // Fails

Why does the static_assert inside the class fail?

Side Qn: Is std::is_default_constructible supposed to fail for the classes with private constructors as discussed in:
std::is_default_constructible<T> error, if constructor is private

Answers to

Why is_default_constructible&lt;Class&gt;::value fails within the same class scope

nr: #1 dodano: 2016-12-27 10:12

The documentation page says that for std::is_default_constructible<T>:

T shall be a complete type, (possibly cv-qualified) void, or an array of unknown bound. Otherwise, the behavior is undefined.

Since you are within your class, the type is not completely defined yet, i guess that's the reason for the difference.

As for the side question, this trait seems to be based on std::is_constructible which seems to mean that if the variable definition

T obj();

is well formed the member constant value equal to true. In all other cases, value is false.

So my understanding of this and my candid name based semantical instinct would say that it should fail if the default constructor is private.

Source Show
◀ Wstecz