std::function<R(Args...)>:: target
From cppreference.net
<
cpp
|
utility
|
functional
|
function
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Function objects
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::function
| Member functions | ||||
|
(until C++17)
|
||||
|
function::target
|
||||
| Non-member functions | ||||
|
(until C++20)
|
||||
| Helper classes | ||||
|
(until C++17)
|
||||
| Deduction guides (C++17) |
|
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++ 标准。
| 缺陷报告 | 应用于 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 2591 | C++11 |
若
T
不满足
Callable
要求则行为未定义
|
行为已定义(始终返回
nullptr
)
|
参见
|
获取存储目标的
typeid
(公开成员函数) |