-
Notifications
You must be signed in to change notification settings - Fork 80
/
G23_Function.cpp
44 lines (33 loc) · 1.21 KB
/
G23_Function.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**************************************************************************************************
*
* \file G23_Function.cpp
* \brief Guideline 23: Prefer a Value-Based Implementation of Strategy and Command
*
* Copyright (C) 2022 Klaus Iglberger - All Rights Reserved
*
* This file is part of the supplemental material for the O'Reilly book "C++ Software Design"
* (https://www.oreilly.com/library/view/c-software-design/9781098113155/).
*
**************************************************************************************************/
#include <cstdlib>
#include <functional>
#include <iostream>
void foo( int i )
{
std::cout << "foo: " << i << '\n';
}
int main()
{
// Create a default std::function instance. Calling it results
// in a std::bad_function_call exception
std::function<void(int)> f{};
f = []( int i ){ // Assigning a callable to 'f'
std::cout << "lambda: " << i << '\n';
};
f(1); // Calling 'f' with the integer '1'
auto g = f; // Copying 'f' into 'g'
f = foo; // Assigning a different callable to 'f'
f(2); // Calling 'f' with the integer '2'
g(3); // Calling 'g' with the integer '3'
return EXIT_SUCCESS;
}