std::is_copy_constructible, std::is_trivially_copy_constructible, std::is_nothrow_copy_constructible - cppreference.com

From cppreference.com

Defined in header

<type_traits>

template<classT>structis_copy_constructible; (1) (since C++11)template<classT>structis_trivially_copy_constructible; (2) (since C++11)template<classT>structis_nothrow_copy_constructible; (3) (since C++11)1) 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,constT&>::value.

2) Same as (1), but uses std::is_trivially_constructible<T,constT&>.

3) Same as (1), but uses std::is_nothrow_constructible<T,constT&>.

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

If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.

If the program adds specializations for any of the templates described on this page, the behavior is undefined.

Helper variable templates

template<classT>inlineconstexprboolis_copy_constructible_v=is_copy_constructible<T>::value;(since C++17)template<classT>inlineconstexprboolis_trivially_copy_constructible_v=is_trivially_copy_constructible<T>::value;(since C++17)template<classT>inlineconstexprboolis_nothrow_copy_constructible_v=is_nothrow_copy_constructible<T>::value;(since C++17)Inherited from

std::integral_constant

Member constants

value

[static]

true if T is copy-constructible, false otherwise
(public static member constant)Member functions

operator bool

converts the object to bool, returns value
(public member function)operator()

(C++14)

returns value
(public member function)Member types

Type Definition value_typebooltypestd::integral_constant<bool,value>Possible implementation

template<classT>structis_copy_constructible:std::is_constructible<T,typenamestd::add_lvalue_reference<typenamestd::add_const<T>::type>::type>{};template<classT>structis_trivially_copy_constructible:std::is_trivially_constructible<T,typenamestd::add_lvalue_reference<typenamestd::add_const<T>::type>::type>{};template<classT>structis_nothrow_copy_constructible:std::is_nothrow_constructible<T,typenamestd::add_lvalue_reference<typenamestd::add_const<T>::type>::type>{};Notes

In many implementations, is_nothrow_copy_constructible also checks if the destructor throws because it is effectively noexcept(T(arg)). Same applies to is_trivially_copy_constructible, which, in these implementations, also requires that the destructor is trivial:

GCC bug 51452

,

LWG issue 2116

.

std::is_copy_constructible_v may report true for types that have a copy constructor, but are ill-formed to invoke, such as std::vector<std::mutex>.

Example

Run this code

#include<string>#include<type_traits>structS1{std::stringstr;// member has a non-trivial copy constructor};static_assert(std::is_copy_constructible_v<S1>);static_assert(!std::is_trivially_copy_constructible_v<S1>);structS2{intn;S2(constS2&)=default;// trivial and non-throwing};static_assert(std::is_trivially_copy_constructible_v<S2>);static_assert(std::is_nothrow_copy_constructible_v<S2>);structS3{S3(constS3&)=delete;// explicitly deleted};static_assert(!std::is_copy_constructible_v<S3>);structS4{S4(S4&){};// cannot bind const, hence not a copy-constructible};static_assert(!std::is_copy_constructible_v<S4>);intmain(){}Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 2196

C++11 the behavior was unclear if constT& cannot be formed the value produced is false in this case See also