命名空间
变体
操作

std::is_constructible、 std::is_trivially_constructible、 std::is_nothrow_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, class... Args >
struct is_constructible;
(1) (自 C++11 起)
template< class T, class... Args >
struct is_trivially_constructible;
(2) (自 C++11 起)
template< class T, class... Args >
struct is_nothrow_constructible;
(3) (自 C++11 起)
1) 如果 T 是对象或引用类型,且变量定义 T obj(std::declval<Args>()...); 良构,则提供等于 true 的成员常量 value 。在所有其他情况下, valuefalse
为此检查,变量定义永远不解释为函数声明,且 std::declval 的使用不被认为是 odr-使用访问检查 执行时如同来自不关联于 TArgs 中任何类型的语境。仅考虑变量定义的直接语境的有效性。
2)(1) 相同,但变量定义不调用任何非平凡的操作。为此检查,到 std::declval 的调用被认为是平凡的。
3)(1) 相同,但变量定义为 noexcept

如果 T 或形参包 Args 中的任何类型是不完整类型、 (可能带 cv 限定的) void 或未知边界数组,则行为未定义。

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

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

目录

[编辑] 辅助变量模板

template< class T, class... Args >

inline constexpr bool is_constructible_v =

    is_constructible<T, Args...>::value;
(自 C++17 起)
template< class T, class... Args >

inline constexpr bool is_trivially_constructible_v =

    is_trivially_constructible<T, Args...>::value;
(自 C++17 起)
template< class T, class... Args >

inline constexpr bool is_nothrow_constructible_v =

    is_nothrow_constructible<T, Args...>::value;
(自 C++17 起)

继承自 std::integral_constant

成员常量

value
[静态]
trueT 可从 Args... 构造,否则为 false
(公共静态成员常量)

成员函数

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

成员类型

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

[编辑] 注解

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

[编辑] 示例

#include <iostream>
#include <type_traits>
 
class Foo
{
    int v1;
    double v2;
public:
    Foo(int n) : v1(n), v2() {}
    Foo(int n, double f) noexcept : v1(n), v2(f) {}
};
 
int main()
{
    auto is = [](bool o) { return (o ? "\t" "is " : "\t" "isn't "); };
    std::cout << "Foo ...\n"
              << is(std::is_trivially_constructible_v<Foo, const Foo&>)
              << "Trivially-constructible from const Foo&\n"
              << is(std::is_trivially_constructible_v<Foo, int>)
              << "Trivially-constructible from int\n"
              << is(std::is_constructible_v<Foo, int>)
              << "Constructible from int\n"
              << is(std::is_nothrow_constructible_v<Foo, int>)
              << "Nothrow-constructible from int\n"
              << is(std::is_nothrow_constructible_v<Foo, int, double>)
              << "Nothrow-constructible from int and double\n";
}

输出

Foo ...
        is Trivially-constructible from const Foo&
        isn't Trivially-constructible from int
        is Constructible from int
        isn't Nothrow-constructible from int
        is Nothrow-constructible from int and double

[编辑] 参见

检查类型是否拥有默认构造函数
(类模板) [编辑]
检查类型是否拥有复制构造函数
(类模板) [编辑]
检查类型是否可从右值引用构造
(类模板) [编辑]
指定类型的变量是否可从或绑定到一组实参类型构造
(概念) [编辑]