std::is_trivially_constructible
STD::是[医]可构造的,STD::is[医]琐碎[医]可构造的,STD::is[医]无抛[医]可构造
Defined in header | | |
---|---|---|
template< class T, class... Args > struct is_constructible; | (1) | (since C++11) |
template< class T, class... Args > struct is_trivially_constructible; | (2) | (since C++11) |
template< class T, class... Args > struct is_nothrow_constructible; | (3) | (since C++11) |
1%29T是对象或引用类型以及变量定义。T obj(std::declval<Args>()...是格式良好的,提供成员常量。value等于true.在所有其他情况下,value是false...
为了本检查的目的,变量定义永远不会被解释为函数声明,并且使用std::declval
不被认为是ODR-使用...存取检查执行时,就像从与以下内容无关的上下文中执行。T
中的任何类型Args
.只考虑变量定义的直接上下文的有效性。
2%29与1%29相同,但变量定义不调用任何不平凡的操作。为了本检查的目的,调用std::declval
被认为是微不足道的。
3%29与1%29相同,但变量定义是noexcept
...
T
和参数包中的所有类型。Args
每一个都是一个完整的类型,%28可能是cv-合格%29void
,或者一系列未知的界限。否则,行为就没有定义。
辅助变量模板
template< class T, class... Args > inline constexpr bool is_constructible_v = is_constructible | | (since C++17) |
---|---|---|
template< class T, class... Args > inline constexpr bool is_trivially_constructible_v = is_trivially_constructible<T, Args...>::value; | | (since C++17) |
template< class T, class... Args > inline constexpr bool is_nothrow_constructible_v = is_nothrow_constructible<T, Args...>::value; | | (since C++17) |
继承自STD:积分[医]常量
成员常数
value static | true if T is constructible from Args... , false otherwise (public static member constant) |
---|
成员函数
operator bool | converts the object to bool, returns value (public member function) |
---|---|
operator() (C++14) | returns value (public member function) |
成员类型
Type | Definition |
---|---|
value_type | bool |
type | std::integral_constant<bool, value> |
注记
在许多实现中,is_nothrow_constructible
还检查析构函数是否抛出,因为它有效。noexcept(T(arg))
.同样适用于is_trivially_constructible
,在这些实现中,还要求析构函数是微不足道的:gcc bug 51452lwg第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() {
std::cout << "Foo is ...\n" << std::boolalpha
<< "\tTrivially-constructible from const Foo&? "
<< std::is_trivially_constructible<Foo, const Foo&>::value << '\n'
<< "\tTrivially-constructible from int? "
<< std::is_trivially_constructible<Foo, int>::value << '\n'
<< "\tConstructible from int? "
<< std::is_constructible<Foo, int>::value << '\n'
<< "\tNothrow-constructible from int? "
<< std::is_nothrow_constructible<Foo, int>::value << '\n'
<< "\tNothrow-constructible from int and double? "
<< std::is_nothrow_constructible<Foo, int, double>::value << '\n';
}
二次
产出:
二次
Foo is ...
Trivially-constructible from const Foo&? true
Trivially-constructible from int? false
Constructible from int? true
Nothrow-constructible from int? false
Nothrow-constructible from int and double? true
二次
另见
is_default_constructibleis_trivially_default_constructibleis_nothrow_default_constructible (C++11)(C++11)(C++11) | checks if a type has a default constructor (class template) |
---|---|
is_copy_constructibleis_trivially_copy_constructibleis_nothrow_copy_constructible (C++11)(C++11)(C++11) | checks if a type has a copy constructor (class template) |
is_move_constructibleis_trivially_move_constructibleis_nothrow_move_constructible (C++11)(C++11)(C++11) | checks if a type can be constructed from an rvalue reference (class template) |
uses_allocator (C++11) | checks if the specified type supports uses-allocator construction (class template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。