-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathG22_Example_1.cpp
More file actions
49 lines (38 loc) · 1.1 KB
/
G22_Example_1.cpp
File metadata and controls
49 lines (38 loc) · 1.1 KB
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
45
46
47
48
49
/**************************************************************************************************
*
* \file G22_Example_1.cpp
* \brief Guideline 22: Prefer Value Semantics to Reference Semantics
*
* 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 <iostream>
#include <span>
#include <vector>
void print( std::span<int> s )
{
std::cout << " (";
for( int i : s ) {
std::cout << ' ' << i;
}
std::cout << " )\n";
}
int main()
{
std::vector<int> v{ 1, 2, 3, 4 };
std::vector<int> const w{ v };
std::span<int> const s{ v };
//w[2] = 99; // Compilation error!
s[2] = 99; // Works!
// Prints ( 1 2 99 4 );
print( s );
v = { 5, 6, 7, 8, 9 };
s[2] = 99; // Works!
// Prints ?
print( s );
return EXIT_SUCCESS;
}