namespace std {
template<class F>
function_ref(F*) -> function_ref<F>; // (1)
template<auto c, class F0>
function_ref(constant_wrapper<c, F0>) -> function_ref<see below>; // (2)
template<auto c, class F, class T>
function_ref(constant_wrapper<c, F>, T&&) -> function_ref<see below>; // (3)
}
概要
std::function_refクラステンプレートの型推論補助。
テンプレートパラメータ制約
- (1) :
is_function<F>がtrueであること。 - (2) :
Fをremove_pointer_t<F0>としたとき、is_function<F>がtrueであること。 - (3) : 次のいずれかであること
- 型
Gに対してFがR(G::*)(A...) /*cv*/ & noexcept(E)の形式(参照修飾子&は省略可、Eはbool値)、または - 型
Gとオブジェクト型Mに対してFがM G::*の形式、または - 型
Gに対してFがR(*)(G, A...) noexcept(E)の形式(Eはbool値)であること
- 型
備考
- (2) :
Fをremove_pointer_t<F0>としたとき、型function_ref<F>に推論される。 - (3) : 型
function_ref<R(A...) noexcept(E)>に推論される。FがM G::*の形式のとき、Rはinvoke_result_t<F, T&>、A...は空のパック、Eはtrueとする。- それ以外の形式のときは、テンプレートパラメータ制約の説明を参照。
例
#include <functional>
int ident(int x) { return x; }
struct X {
int data;
int mf(int x) { return x; }
};
int fun(X& obj) { return obj.data; }
int main()
{
// (1) 関数ポインタ
std::function_ref f1{&ident};
// (2) 関数ポインタ/NTTP
std::function_ref f2{std::cw<&ident>};
// (3a) メンバ関数+オブジェクト束縛
X obj{42};
std::function_ref f3a{std::cw<&X::mf>, obj};
// (3b) メンバ変数+オブジェクト束縛
std::function_ref f3b{std::cw<&X::data>, obj};
// (3c) 関数ポインタ+第1引数束縛
std::function_ref f3c{std::cw<&fun>, obj};
}
出力
バージョン
言語
- 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- (2), (3)の推論補助が受け取るタグを
constant_arg_tからconstant_wrapper(変数テンプレートstd::cw)へ置き換えた
- (2), (3)の推論補助が受け取るタグを
- LWG Issue 4425 CTAD
function_refof data member pointer should produce noexcept signature- (3)でメンバ変数ポインタを渡した場合の推論結果が
noexceptシグニチャ(Eがtrue)になるよう修正された
- (3)でメンバ変数ポインタを渡した場合の推論結果が