A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/functional/function/target/ below:

public member function

<functional>

std::function::target
template <class TargetType>       TargetType* target() noexcept;template <class TargetType> const TargetType* target() const noexcept;

Get pointer to target

Returns a pointer to the callable object stored in the function object.

Because function is a polymorphic wrapper class, it is unaware of the static type of its target callable object, and thus the template parameter TargetType must be explicitly specified.

TargetType shall match the target type, so that typeid(TargetType)==target_type(). Otherwise, the function always returns a null pointer.



Parameters none

Return value A pointer to the target callable object, if typeid(TargetType) compares equal to the value returned by member target_type.
Otherwise, a null pointer.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// function::target example
#include <iostream>     // std::cout, std::boolalpha
#include <functional>   // std::function, std::plus, std::minus

int my_plus (int a, int b) {return a+b;}
int my_minus (int a, int b) {return a-b;}

int main () {
  std::function<int(int,int)> foo = my_plus;
  std::function<int(int,int)> bar = std::plus<int>();

  // calling using functional form:
  std::cout << foo(100,20) << '\n';
  std::cout << bar(100,20) << '\n';

  // calling by invoking target:
  std::cout << (*foo.target<int(*)(int,int)>())(100,20) << '\n';
  std::cout << (*bar.target<std::plus<int>>())(100,20) << '\n';

  // changing target directly:
  *foo.target<int(*)(int,int)>() = &my_minus;
  std::cout << foo(100,20) << '\n';

  return 0;
}

Output:


Data races Both the object and its target are accessed.
The pointer returned can be used to access or modify the target callable object.

Exception safetyNo-throw guarantee: this member function never throws exceptions.

See also
function::operator()
Call target (public member function)
function::target_type
Target type_info (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4