在线文档教程
C++
应用 | Utilities

std::is_nothrow_assignable

STD::是[医]可分配的,STD::IS[医]琐碎[医]可分配的,STD::IS[医]无抛[医]可转让

Defined in header
template< class T, class U > struct is_assignable;(1)(since C++11)
template< class T, class U > struct is_trivially_assignable;(2)(since C++11)
template< class T, class U > struct is_nothrow_assignable;(3)(since C++11)

1%29如果表达式std::declval<T>()=std::declval<U>()在未计算的上下文中格式良好,则提供成员常量。value平等true.否则,value是false...存取检查执行时,就好像是从与任何类型无关的上下文中执行的。

2%29与%281%29相同,但赋值表达式的计算不会调用任何不简单的操作。为了这张支票的目的,打电话给std::declval被认为是琐碎的,而不是ODR-使用成std::declval...

3%29与%281%29相同,但赋值表达式的计算不会调用除“否”以外的任何操作。

TU每一个都是一个完整的类型,%28可能是cv-合格%29void,或者一系列未知的界限。否则,行为就没有定义。

辅助变量模板

template< class T, class U > inline constexpr bool is_assignable_v = is_assignable::value;(since C++17)
template< class T, class U > inline constexpr bool is_trivially_assignable_v = is_trivially_assignable<T, U>::value;(since C++17)
template< class T, class U > inline constexpr bool is_nothrow_assignable_v = is_nothrow_assignable<T, U>::value;(since C++17)

继承自STD:积分[医]常量

成员常数

value statictrue if T is assignable from U , false otherwise (public static member constant)

成员函数

operator boolconverts the object to bool, returns value (public member function)
operator() (C++14)returns value (public member function)

成员类型

TypeDefinition
value_typebool
typestd::integral_constant<bool, value>

注记

此特性不检查赋值表达式的直接上下文之外的任何内容:如果使用T或U将触发模板的专门化,生成隐式定义的特殊成员函数等,并且这些函数都有错误,即使在以下情况下,实际分配也可能不会编译。std::is_assignable<T,U>::value编译并计算为true...

二次

#include <iostream> #include <string> #include <type_traits> struct Ex1 { int n; }; int main() { std::cout << std::boolalpha << "int is assignable from int? " << std::is_assignable<int, int>::value << '\n' // 1 = 1; wouldn't compile << "int& is assignable from int? " << std::is_assignable<int&, int>::value << '\n' // int a; a = 1; works << "int is assignable from double? " << std::is_assignable<int, double>::value << '\n' << "int& is nothrow assignable from double? " << std::is_nothrow_assignable<int&, double>::value << '\n' << "string is assignable from double? " << std::is_assignable<std::string, double>::value << '\n' << "Ex1& is trivially assignable from const Ex1&? " << std::is_trivially_assignable<Ex1&, const Ex1&>::value << '\n'; }

二次

产出:

二次

int is assignable from int? false int& is assignable from int? true int is assignable from double? false int& is nothrow assignable from double? true string is assignable from double? true Ex1& is trivially assignable from const Ex1&? true

二次

另见

is_copy_assignableis_trivially_copy_assignableis_nothrow_copy_assignable (C++11)(C++11)(C++11)checks if a type has a copy assignment operator (class template)
is_move_assignableis_trivially_move_assignableis_nothrow_move_assignable (C++11)(C++11)(C++11)checks if a type has a move assignment operator (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/type/is[医]可转让