std:: is_constructible, std:: is_trivially_constructible, std:: is_nothrow_constructible
|
定义于头文件
<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 起) |
T
是对象或引用类型,且变量定义
T obj
(
std::
declval
<
Args
>
(
)
...
)
;
是良构的,则提供等于
true
的成员常量
value
。其他所有情况下,
value
为
false
。
为此检查之目的,该变量定义始终不会被解释为函数声明,且 std::declval 的使用不被视为 ODR 使用 。 访问检查 的执行环境被视为与
T
及
Args
中任何类型无关的上下文。仅考虑变量定义直接语境的合法性。
noexcept
限定符。
如果
T
或参数包
Args
中的任何类型不是完整类型、(可能带有 cv 限定符的)
void
,或未知边界的数组,则行为未定义。
如果上述模板的实例化直接或间接依赖于不完整类型,且该实例化在该类型被假设完成时可能产生不同结果,则行为未定义。
如果程序对本页面描述的任何模板添加特化,则行为未定义。
目录 |
辅助变量模板
|
template
<
class
T,
class
...
Args
>
inline
constexpr
bool
is_constructible_v
=
|
(自 C++17 起) | |
|
template
<
class
T,
class
...
Args
>
inline
constexpr
bool
is_trivially_constructible_v
=
|
(自 C++17 起) | |
|
template
<
class
T,
class
...
Args
>
inline
constexpr
bool
is_nothrow_constructible_v
=
|
(自 C++17 起) | |
继承自 std:: integral_constant
成员常量
|
value
[static]
|
若
T
可从
Args...
构造则为
true
,否则为
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
参见
|
(C++11)
(C++11)
(C++11)
|
检查类型是否拥有默认构造函数
(类模板) |
|
(C++11)
(C++11)
(C++11)
|
检查类型是否拥有拷贝构造函数
(类模板) |
|
(C++11)
(C++11)
(C++11)
|
检查类型是否可以从右值引用构造
(类模板) |
|
(C++20)
|
指定该类型的变量可以从一组实参类型构造或绑定
(概念) |