function::target

如果存储的可调用的对象是可调用的规定,测试。

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

参数

  • Fty2
    测试目标可调用的对象类型。

备注

该类型 Fty2 必须是可调用的参数类型 T1, T2, ..., TN 和返回类型 Ret。如果 target_type() == typeid(Fty2),成员模板函数返回目标对象的地址;否则,则返回0。

类型 Fty2 为参数类型 T1, T2, ..., TN 和返回类型 Ret 是可调用的,则,对于lvalue类型 Fty2, T1, T2, ..., TNfn, t1, t2, ..., tN,INVOKE(fn, t1, t2, ..., tN),分别为限定,则为; Ret 不是 void,转换为 Ret。

示例

 

// std_tr1__functional__function_target.cpp 
// compile with: /EHsc 
#include <functional> 
#include <iostream> 
 
int neg(int val) 
    { 
    return (-val); 
    } 
 
int main() 
    { 
    typedef int (*Myfun)(int); 
    std::function<int (int)> fn0(neg); 
    std::cout << std::boolalpha << "empty == " << !fn0 << std::endl; 
    std::cout << "no target == " << (fn0.target<Myfun>() == 0) << std::endl; 
 
    Myfun *fptr = fn0.target<Myfun>(); 
    std::cout << "val == " << (*fptr)(3) << std::endl; 
 
    std::function<int (int)> fn1; 
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl; 
    std::cout << "no target == " << (fn1.target<Myfun>() == 0) << std::endl; 
 
    return (0); 
    } 
 
  

要求

标头: <functional>

命名空间: std

请参见

参考

function Class

function::target_type