首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::function::target

template< class T > T* target();

(1)

(since C++11)

template< class T > const T* target() const;

(2)

(since C++11)

返回指向存储的可调用函数目标的指针。

参数

%280%29

返回值

指向存储函数的指针target_type() == typeid(T),否则为空指针。

例外

noexcept规格:

noexcept

二次

代码语言:javascript
复制
#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));
}

二次

产出:

二次

代码语言:javascript
复制
test function: it is plus
test function: it is minus
test function: it is the function f
test function: it is the function g

二次

另见

target_type

obtains the typeid of the stored target (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com