std::is_assignable, std::is_trivially_assignable, std::is_nothrow_assignable - cppreference.com

From cppreference.com

Defined in header

<type_traits>

template<classT,classU>structis_assignable; (1) (since C++11)template<classT,classU>structis_trivially_assignable; (2) (since C++11)template<classT,classU>structis_nothrow_assignable; (3) (since C++11)1) If the expression std::declval<T>()=std::declval<U>() is well-formed in unevaluated context, provides the member constant value equal to true. Otherwise, value is false.

Access checks

are performed as if from a context unrelated to either type.

2) Same as (1), but the evaluation of the assignment expression will not call any operation that is not trivial. For the purposes of this check, a call to

std::declval

is considered trivial and not considered an

odr-use

of

std::declval

.

3) Same as (1), but the evaluation of the assignment expression will not call any operation that is not noexcept.

If T or U is not a complete type, (possibly cv-qualified) void, or an array of unknown bound, the behavior is undefined.

If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.

If the program adds specializations for any of the templates described on this page, the behavior is undefined.

Helper variable templates

template<classT,classU>constexprboolis_assignable_v=is_assignable<T,U>::value;(since C++17)template<classT,classU>constexprboolis_trivially_assignable_v=is_trivially_assignable<T,U>::value;(since C++17)template<classT,classU>constexprboolis_nothrow_assignable_v=is_nothrow_assignable<T,U>::value;(since C++17)Inherited from

std::integral_constant

Member constants

value

[static]

true if T is assignable from U, false otherwise
(public static member constant)Member functions

operator bool

converts the object to bool, returns value
(public member function)operator()

(C++14)

returns value
(public member function)Member types

Type Definition value_typebooltypestd::integral_constant<bool,value>Notes

This trait does not check anything outside the immediate context of the assignment expression: if the use of T or U would trigger template specializations, generation of implicitly-defined special member functions etc, and those have errors, the actual assignment may not compile even if std::is_assignable<T,U>::value compiles and evaluates to true.

Example

Run this code

#include<string>#include<type_traits>structC{intn;};static_assert(std::is_assignable<int,int>::value==false&&// Error, 1 = 1 won't compilestd::is_assignable<int&,int>::value==true&&// OK, int a; a = 1;std::is_assignable<int,double>::value==false&&// Error, 1 = 1.0;std::is_nothrow_assignable_v<int&,double>==true&&// OK, int a; a = 1.0;// uses implicit conversion double -> char, then string::operator=(char)std::is_assignable_v<std::string,double>==true&&// OK, s = 69.6; s == 'E'std::is_trivially_assignable_v<C&,constC&>==true);intmain(){}See also