std::function::target_type

From cppreference.com
< cpp‎ | utility‎ | functional‎ | function
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
Function objects
Function wrappers
(C++11)
(C++11)
(C++17)
Bind
(C++11)
(C++11)
(C++11)
Reference wrappers
(C++11)(C++11)
Operator wrappers
Negators
(deprecated)
(deprecated)
(deprecated)
(deprecated)
Searchers
Old binders and adaptors
(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++17)(until C++17)(until C++17)
(until C++17)(until C++17)
(until C++17)(until C++17)
 
 
const std::type_info& target_type() const;
(since C++11)

Returns the type of the stored function.

Contents

[edit] Parameters

(none)

[edit] Return value

typeid(T) if the stored function has type T, otherwise typeid(void)

[edit] Exceptions

noexcept specification:  
noexcept
  

[edit] Example

#include <functional>
#include <iostream>
 
int f(int a) { return -a; }
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';
}

Possible output:

int (*)(int)
main::$_0

[edit] See also

obtains a pointer to the stored target
(public member function)