std::function<R(Args...)>::target
From cppreference.com
< cpp | utility | functional | function
| 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を取得する (public member function) |