namespace std {
template <class T, class U>
struct is_nothrow_assignable;
template <class T, class U>
inline constexpr bool is_nothrow_assignable_v
= is_nothrow_assignable<T, U>::value; // C++17
}
概要
型T
が型U
から代入可能で、かつ代入操作が例外を投げないかを調べる。
要件
型T
および型U
が完全型であるか、const
/volatile
修飾された(あるいはされていない)void
か、要素数不明の配列型でなければならない。
効果
is_nothrow_assignable
は、型T
が型U
から例外を投げない保証のもとに代入可能であるならばtrue_type
から派生し、そうでなければfalse_type
から派生する。
is_assignable<T, U>::value == true
かつ、いかなる例外を投げないならば、例外を投げない代入が可能であると判断される。
例
#include <type_traits>
struct X {
X& operator=(const X&) noexcept
{ return *this; }
X& operator=(X&&) noexcept
{ return *this; }
};
static_assert(
std::is_nothrow_assignable<int&, int>::value == true,
"int is nothrow assignable");
static_assert(
std::is_nothrow_assignable<X&, const X&>::value == true,
"X is nothrow copy assignable");
static_assert(
std::is_nothrow_assignable<X&, X&&>::value == true,
"X is nothrow move assignable");
int main() {}
24
#include <type_traits>
struct X {
X& operator=(const X&) noexcept
{ return *this; }
X& operator=(X&&) noexcept
{ return *this; }
};
static_assert(
std::is_nothrow_assignable<int&, int>::value == true,
"int is nothrow assignable");
static_assert(
std::is_nothrow_assignable<X&, const X&>::value == true,
"X is nothrow copy assignable");
出力
バージョン
言語
- C++11
処理系
- Clang: 3.0 ✅
- GCC: 4.7.3 ✅
- Visual C++: 2012 ✅, 2013 ✅, 2015 ✅