std::function<R(Args...)>::target
From cppreference.com
< cpp | utility | functional | function
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Function objects
Old binders and adaptors
(C++11)
(C++23)
(C++26)
(C++26)
(C++11)
(C++11)
(C++20)(C++23)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++20)(C++20)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
std::function
template< class T >
T* target() noexcept;
(1)
(since C++11)
T* target() noexcept;
template< class T >
const T* target() const noexcept;
(2)
(since C++11)
const T* target() const noexcept;
Returns a pointer to the stored callable function target.
[edit] Parameters
(none)
[edit] Return value
A pointer to the stored function if target_type() == typeid(T), otherwise a null pointer.
[edit] Example
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)); }
Output:
test function: it is plus test function: it is minus test function: it is the function f test function: it is the function g
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2591 | C++11 | the behavior is undefined if T is not Callable
|
behavior is defined (always returns nullptr )
|