C++ Library Extensions
2022.12.09
To help learn modern C++ programming
14-invoke.cpp
Go to the documentation of this file.
1
#include <
tpf_output.hpp
>
2
3
tpf::sstream
stream
;
4
auto
endl
=
tpf::endl
;
5
6
int
sum
(
int
a,
int
b)
7
{
8
return
a + b;
9
}
10
11
struct
Product
12
{
13
int
operator()
(
int
a,
int
b)
const
14
{
15
return
a * b;
16
}
17
18
int
summation
(
int
a,
int
b,
int
c)
19
{
20
return
a + b + c;
21
}
22
23
};
24
25
void
test_invoke
()
26
{
27
28
stream
<<
"sum(2, 3) = "
<< std::invoke(
sum
, 2, 3) <<
endl
;
29
30
stream
<<
"Product(3, 4) = "
31
<< std::invoke(
Product
(), 3, 4) <<
endl
;
32
33
stream
<<
"&Product::summation(1, 2, 3) = "
34
<< std::invoke(&
Product::summation
,
Product
{}, 1, 2, 3) <<
endl
;
35
36
stream
<<
"Lambda(3, 4) = "
37
<< std::invoke([](
int
a,
int
b){
return
a * b; } , 3, 4) <<
endl
;
38
39
auto
closure = [](
int
a,
int
b,
int
c)
40
{
41
return
(a + b) * c;
42
} ;
43
44
stream
<<
"closure(3, 3, 2) = "
<< std::invoke(closure, 3, 3, 2) <<
endl
;
45
}
46
47
int
main
()
48
{
49
test_invoke
();
50
}
stream
tpf::sstream stream
Definition:
14-invoke.cpp:3
sum
int sum(int a, int b)
Definition:
14-invoke.cpp:6
endl
auto endl
Definition:
14-invoke.cpp:4
main
int main()
Definition:
14-invoke.cpp:47
test_invoke
void test_invoke()
Definition:
14-invoke.cpp:25
tpf::output::string_stream
Definition:
tpf_output.hpp:844
tpf::endl
constexpr auto endl
Definition:
tpf_output.hpp:973
Product
Definition:
14-invoke.cpp:12
Product::operator()
int operator()(int a, int b) const
Definition:
14-invoke.cpp:13
Product::summation
int summation(int a, int b, int c)
Definition:
14-invoke.cpp:18
tpf_output.hpp
Stream output operators << are implemented.
CppExtension
graph
14-invoke.cpp
Generated by
1.9.4