constexpr function_ref& operator=(const function_ref&) noexcept = default; // (1)
template<class T> function_ref& operator=(T) = delete; // (2)
概要
コピー代入演算子。
テンプレートパラメータ制約
- (2) : 以下の制約をみたすとき、代入演算子はdelete宣言される
Tがfunction_refと同一型ではなく、かつ- 説明専用の
is-convertible-from-specialization<T>(コンストラクタを参照)がfalseであり、かつ is_pointer_v<T>がfalseであり、かつTがconstant_wrapperの特殊化でないこと
効果
- (1) : コピー代入。
戻り値
*this
例
#include <iostream>
#include <functional>
int ident(int x)
{ return x; }
int twice(int x)
{ return x * 2; }
int main()
{
std::function_ref<int(int)> f = ident;
std::function_ref<int(int)> g = twice;
// コピー代入
f = g;
std::cout << f(1) << std::endl;
}
出力
2
バージョン
言語
- C++26
処理系
- Clang: 24 ✅
- GCC: 16.1 ✅
- ICC: ??
- Visual C++: ??
参照
- P0792R14
function_ref: a type-erased callable reference - P3774R1 Rename
std::nontype, and make it broadly usefulstd::nontypeをstd::constant_argに改名した。この設計はのちにP3948R1で置き換えられた
- P3948R1
constant_wrapperis the only tool needed for passing constant expressions via function arguments- delete宣言される代入演算子(2)の制約で、除外対象を
constant_arg_tの特殊化からconstant_wrapperの特殊化へ置き換えた
- delete宣言される代入演算子(2)の制約で、除外対象を
- P3961R1 Less double indirection in
function_ref(RU-220)- delete宣言される代入演算子(2)の制約に、説明専用の
is-convertible-from-specializationの条件を追加した
- delete宣言される代入演算子(2)の制約に、説明専用の