std::function<R(Args...)>::target
提供: cppreference.com
<tbody>
</tbody>
template< class T > T* target() noexcept; |
(1) | (C++11以上) |
template< class T > const T* target() const noexcept; |
(2) | (C++11以上) |
格納されている callable な関数ターゲットを指すポインタを返します。
引数
(なし)
戻り値
target_type() == typeid(T) であれば、格納されている関数を指すポインタ。 そうでなければ、ヌルポインタ。
例
Run this code
#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
関連項目
格納されているターゲットの typeid を取得します (パブリックメンバ関数) |