std::function<R(Args...)>::target_type
来自 cppreference.cn
< cpp | utility | functional | function
const std::type_info& target_type() const noexcept; |
(C++11 起) | |
返回存储的函数类型。
目录 |
[编辑] 参数
(无)
[编辑] 返回值
如果存储的函数类型为 T
,则返回 typeid(T),否则返回 typeid(void)。
[编辑] 示例
运行此代码
#include <functional> #include <iostream> int f(int a) { return -a; } void g(double) {} int main() { // fn1 and fn2 have the same type, but their targets do not std::function<int(int)> fn1(f), fn2([](int a) {return -a;}); std::cout << fn1.target_type().name() << '\n' << fn2.target_type().name() << '\n'; // since C++17 deduction guides (CTAD) can avail std::cout << std::function{g}.target_type().name() << '\n'; }
可能的输出
PFiiE Z4mainEUliE_ PFvdE
[编辑] 参阅
获取指向存储目标的指针 (公有成员函数) | |
包含某些类型信息的类,由 typeid 运算符返回 (类) | |
typeid | 查询类型信息,返回表示该类型的 std::type_info 对象(运算符) |