std::is_ass..._assignable(3)
π NAME π
std::is_assignable, std::is_trivially_assignable, std::is_nothrow_assignable β hi bestie, you found the page for these three type-trait girlies. The original NAME line also has the dash and then repeats the exact same three names, so full transparency: std::is_assignable,std::is_trivially_assignable,std::is_nothrow_assignable β std::is_assignable,std::is_trivially_assignable,std::is_nothrow_assignable. This is from the C++ Standard Libary manual, page std::is_ass..._assignable(3). π
β¨ Synopsis β¨
Defined in header <type_traits>.
template< class T, class U > (1) (since C++11)
struct is_assignable;
template< class T, class U > (2) (since C++11)
struct is_trivially_assignable;
template< class T, class U > (3) (since C++11)
struct is_nothrow_assignable;
Okay so the numbered tea β:
(1)β If the expressionstd::declval<T>() = std::declval<U>()is well-formed in unevaluated context, provides the member constantvalueequal totrue. Otherwise,valueisfalse. 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 tostd::declvalis considered trivial and not considered an odr-use ofstd::declval.(3)β Same as(1), but the evaluation of the assignment expression will not call any operation that is notnoexcept.
π¨ IfTorUis 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.
π Member constants
valueβtrueifTis assignable fromU,falseotherwise.[static](public static member constant)
π§ Member functions
operator boolβ converts the object tobool, returnsvalue(public member function).operator()β returnsvalue(C++14) (public member function).
π¦ Member types
value_typeβbooltypeβstd::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.
Lowkey important caveat, no cap.
πΈ Example
// Run this code β okay bestie, here's the code:
#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';
}
Output:
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
π See also
is_copy_assignable,is_trivially_copy_assignable,is_nothrow_copy_assignableβ checks if a type has a copy assignment operator; all three are(C++11)(class template).is_move_assignable,is_trivially_move_assignable,is_nothrow_move_assignableβ checks if a type has a move assignment operator; all three are(C++11)(class template).assignable_fromβ specifies that a type is assignable from another type;(C++20)(concept).
Okay that's the whole page, bestie. This iconic little manual came from http://cppreference.com, version/date 2024.06.10, and it's std::is_ass..._assignable(3) forever. mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms