std::function<R(Args...)>::target
ja.cppreference.net より
template< class T >
T* target() noexcept;
|
(1) | (C++11 以降) |
template< class T >
const T* target() const noexcept;
|
(2) | (C++11 以降) |
格納されている呼び出し可能な関数ターゲットへのポインタを返します。
パラメータ
(なし)
戻り値
格納された関数へのポインタ( target_type ( ) == typeid ( T ) の場合)、それ以外の場合はヌルポインタ。
例
このコードを実行
#include <functional> #include <iostream> int f(int, int) { return 1; } int g(int, int) { return 2; } void test(std::function<int(int, int)> const& arg) { std::cout << "test function: "; if (arg.target<std::plus<int>>()) std::cout << "it is plus\n"; if (arg.target<std::minus<int>>()) std::cout << "it is minus\n"; int (*const* ptr)(int, int) = arg.target<int(*)(int, int)>(); if (ptr && *ptr == f) std::cout << "it is the function f\n"; if (ptr && *ptr == g) std::cout << "it is the function g\n"; } int main() { test(std::function<int(int, int)>(std::plus<int>())); test(std::function<int(int, int)>(std::minus<int>())); test(std::function<int(int, int)>(f)); test(std::function<int(int, int)>(g)); }
出力:
test function: it is plus test function: it is minus test function: it is the function f test function: it is the function g
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2591 | C++11 |
T
が
Callable
でない場合、動作は未定義
|
動作は定義される(常に
nullptr
を返す)
|
関連項目
|
格納されたターゲットの
typeid
を取得する
(公開メンバ関数) |