Search code examples
c++type-traits

Can we assume std::is_default_constructible<T> and std::is_constructible<T> to be equal?


Pretty short question here:

Will std::is_default_constructible<T> and std::is_constructible<T> give the same result? And what about to the new concepts std::default_initializable and std::constructible_from.

It might be important to know the distinctions when making templated factory or emplace functions.


Solution

  • So I finally got to reading the specification. Here's what I found:

    20.15.4.3 is_default_constructible<T>:

    As pointed out by @Raymon Chen in the comments: true precisely when is_­constructible<T> holds true.

    18.4.11 constructible_from<T>:

    Is defined in term of is_constructible<T>, but also poses the additional requirement destructible<T>.

    18.4.12 default_initializable<T>:

    Is defined in term of constructible_from<T>, but also requires T{} to be well-formed. Defining a variable of type T must be possible too; I.e. the statement T var{}; should be well-formed.

    Source: N4861 Working Draft, Standard for Programming Language C++