GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||
GeezerVison is showing cpprefjp.github.io — std::function_ref - cpprefjp C++日本語リファレンス
class template <functional> std::function_ref(C++26)概要
下記全ての組み合わせ(4種類)に対して、クラステンプレートの部分特殊化が提供される。
|
| 名前 | 説明 | 対応バージョン |
|---|---|---|
(constructor) |
コンストラクタ | C++26 |
(destructor) |
デストラクタ | C++26 |
operator= |
代入演算子 | C++26 |
operator() |
関数呼び出し | C++26 |
| 名前 | 説明 | 対応バージョン |
|---|---|---|
(deduction_guide) |
クラステンプレートの推論補助 | C++26 |
#include <functional>
#include <iostream>
#include <utility>
// 呼び出し可能な何かを受け取る高階関数
int hof(std::function_ref<int(int)> fn)
{
return fn(2);
}
int add(int x) { return x + 1; }
struct Calc {
int x_;
int eval(int y) {
return x_ * y;
}
};
int main()
{
// 通常関数を指定
std::cout << hof(add) << std::endl;
// ラムダ式を指定
std::cout << hof([](int x) { return x * 2; }) << std::endl;
// オブジェクト束縛済みメンバ関数を指定
Calc obj{ 3 };
std::cout << hof({std::cw<&Calc::eval>, obj}) << std::endl;
}
3
4
6
functionmove_only_functioncopyable_functionfunction_ref: a type-erased callable referencestd::nontype, and make it broadly usefulstd::nontypeをstd::constant_argに改名した。この設計はのちにP3948R1で置き換えられたconstant_wrapper is the only tool needed for passing constant expressions via function argumentsconstant_argからconstant_wrapper(変数テンプレートstd::cw)へ置き換えた。これによりメンバ関数・メンバ変数の参照や第1引数の束縛はstd::cwで指定するfunction_ref (RU-220)function_refから構築する際の二重の間接呼び出しを回避するよう、コンストラクタと代入演算子の仕様を変更した