命名空间
变体
操作

std::is_copy_constructible, std::is_trivially_copy_constructible, std::is_nothrow_copy_constructible

来自 cppreference.cn
< cpp‎ | types
 
 
元编程库
类型特征
类型类别
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)(在 C++26 中弃用)
(C++11)(直到 C++20*)
(C++11)(在 C++20 中弃用)
(C++11)
类型特征常量
元函数
(C++17)
支持的操作
关系和属性查询
类型修改
(C++11)(C++11)(C++11)
类型转换
(C++11)(在 C++23 中弃用)
(C++11)(在 C++23 中弃用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
编译时有理算术
编译时整数序列
 
定义于头文件 <type_traits>
template< class T >
struct is_copy_constructible;
(1) (自 C++11 起)
template< class T >
struct is_trivially_copy_constructible;
(2) (自 C++11 起)
template< class T >
struct is_nothrow_copy_constructible;
(3) (自 C++11 起)
 类型特征  成员常量 value 的值
T可引用类型  T 不是可引用类型 
(1) std::is_constructible<T, const T&>::value false
(2) std::is_trivially_constructible<T, const T&>::value
(3) std::is_nothrow_constructible<T, const T&>::value

如果 T 不是完整类型,(可能带 cv 限定符的)void,或未知边界的数组,则行为未定义。

如果上述模板的实例化直接或间接地依赖于不完整类型,并且如果假设该类型是完整的,则该实例化可能产生不同的结果,则行为未定义。

如果程序为此页面上描述的任何模板添加了特化,则行为未定义。

目录

[编辑] 辅助变量模板

template< class T >

inline constexpr bool is_copy_constructible_v =

    is_copy_constructible<T>::value;
(自 C++17 起)
template< class T >

inline constexpr bool is_trivially_copy_constructible_v =

    is_trivially_copy_constructible<T>::value;
(自 C++17 起)
template< class T >

inline constexpr bool is_nothrow_copy_constructible_v =

    is_nothrow_copy_constructible<T>::value;
(自 C++17 起)

继承自 std::integral_constant

成员常量

value
[静态]
true 如果 T 是可复制构造的,否则为 false
(公共静态成员常量)

成员函数

operator bool
将对象转换为 bool,返回 value
(公共成员函数)
operator()
(C++14)
返回 value
(公共成员函数)

成员类型

类型 定义
value_type bool
type std::integral_constant<bool, value>

[编辑] 可能的实现

template<class T>
struct is_copy_constructible :
    std::is_constructible<T, typename std::add_lvalue_reference<
        typename std::add_const<T>::type>::type> {};
 
template<class T>
struct is_trivially_copy_constructible :
    std::is_trivially_constructible<T, typename std::add_lvalue_reference<
        typename std::add_const<T>::type>::type> {};
 
template<class T>
struct is_nothrow_copy_constructible :
    std::is_nothrow_constructible<T, typename std::add_lvalue_reference<
        typename std::add_const<T>::type>::type> {};

[编辑] 注解

在许多实现中,is_nothrow_copy_constructible 也检查析构函数是否抛出异常,因为它实际上是 noexcept(T(arg))。同样适用于 is_trivially_copy_constructible,在这些实现中,它也要求析构函数是平凡的:GCC bug 51452LWG issue 2116

[编辑] 示例

#include <string>
#include <type_traits>
 
struct S1
{
    std::string str; // member has a non-trivial copy constructor
};
static_assert(std::is_copy_constructible_v<S1>);
static_assert(!std::is_trivially_copy_constructible_v<S1>);
 
struct S2
{
    int n;
    S2(const S2&) = default; // trivial and non-throwing
};
static_assert(std::is_trivially_copy_constructible_v<S2>);
static_assert(std::is_nothrow_copy_constructible_v<S2>);
 
struct S3
{
    S3(const S3&) = delete; // explicitly deleted
};
static_assert(!std::is_copy_constructible_v<S3>);
 
struct S4
{
    S4(S4&) {}; // cannot bind const, hence not a copy-constructible
};
static_assert(!std::is_copy_constructible_v<S4>);
 
int main() {}

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 2196 C++11 如果无法形成 const T&,则行为不明确 在这种情况下,产生的值为 false

[编辑] 参见

检查类型是否具有特定参数的构造函数
(类模板) [编辑]
检查类型是否具有默认构造函数
(类模板) [编辑]
检查是否可以从右值引用构造类型
(类模板) [编辑]
指定类型对象可以复制构造和移动构造
(概念) [编辑]