Search code examples
c++c++11standardstype-traitscompile-time-constant

Why does std::is_copy_constructible not behave as expected?


#include <type_traits>

int main()
{
    std::is_constructible_v<int&, const int&>; // false, as expected.
    std::is_copy_constructible_v<int&>; // true, NOT as expected!
}

According to cppref:

If T is an object or reference type and the variable definition T obj(std::declval()...); is well-formed, provides the member constant value equal to true. In all other cases, value is false.

std::is_copy_constructible_v<int&> should give the same result as std::is_constructible_v<int&, const int&> does; however, clang 7.0 gives different results as shown above.

Does this behavior conform to the C++ standards?


Solution

  • What the reference for is_copy_constructible states is:

    If T is not a referenceable type (i.e., possibly cv-qualified void or a function type with a cv-qualifier-seq or a ref-qualifier), provides a member constant value equal to false. Otherwise, provides a member constant value equal to std::is_constructible<T, const T&>::value.

    So, here is_copy_constructible<T>::value is the same as std::is_constructible<T, const T&>::value.

    So in your case:

    std::is_constructible<int, const int&>::value will be the same as std::is_copy_constructible_v<int>.

    See DEMO