namespace std {
template <class T>
struct is_nothrow_copy_assignable;
template <class T>
inline constexpr bool is_nothrow_copy_assignable_v
= is_nothrow_copy_assignable<T>::value; // C++17
}
概要
型T
がコピー代入可能で、かつ代入操作が例外を投げないかを調べる。
要件
型T
は完全型であるか、const
/volatile
修飾された(あるいはされていない)void
か、要素数不明の配列型でなければならない。
効果
is_nothrow_copy_assignable
は、型T
が例外を投げない保証のもとにコピー代入可能であるならばtrue_type
から派生し、そうでなければfalse_type
から派生する。
以下の条件がtrue
である場合に、例外を投げないコピー代入が可能であると見なされる:
- C++11 :
is_nothrow_assignable<T&, const T&>::value == true
- C++14 : 参照可能な型
T
に対しては、is_nothrow_assignable<T&, const T&>::value == true
と同じ結果となり、それ以外はfalse
と見なされる。
例
#include <type_traits>
// nothrow copy assignable
struct A {};
// nothrow copy assignable
struct B {
B& operator=(const B&) noexcept
{ return *this; }
};
// not copy assignable
struct C {
C& operator=(const C&)
{ return *this; }
};
// not nothrow copy assignable
struct D {
D& operator=(const D&) noexcept(false)
{ return *this; }
};
static_assert(
std::is_nothrow_copy_assignable<int>::value == true,
"int is nothrow copy assignable");
static_assert(
std::is_nothrow_copy_assignable<A>::value == true,
"A is nothrow copy assignable");
static_assert(
std::is_nothrow_copy_assignable<B>::value == true,
"B is nothrow copy assignable");
static_assert(
std::is_nothrow_copy_assignable<C>::value == false,
"C is not nothrow copy assignable");
static_assert(
std::is_nothrow_copy_assignable<D>::value == false,
"D is not nothrow copy assignable");
int main() {}
出力
バージョン
言語
- C++11
処理系
- Clang: 3.0 ✅
- GCC: 4.7.3 ✅
- Visual C++: 2012 ✅, 2013 ✅, 2015 ✅
- 2012~2013には、提案時の名前である
has_nothrow_assign
,has_nothrow_copy_assign
も存在する。
- 2012~2013には、提案時の名前である
参照
- LWG Issue 2196. Specification of
is_*[copy/move]_[constructible/assignable]
unclear for non-referencable types- C++11では、この型特性が参照型に対してどのような振る舞いになるのか不明確であったため、C++14で明確化された。
- P0006R0 Adopt Type Traits Variable Templates from Library Fundamentals TS for C++17