-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructuredBindings.cpp
More file actions
51 lines (39 loc) · 1.24 KB
/
StructuredBindings.cpp
File metadata and controls
51 lines (39 loc) · 1.24 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
50
51
/**
* \file StructuredBindings.cpp
* \brief Structured bindings
*
* A proposal for de-structuring initialization, that would allow writing auto [ x, y, z ] = expr;
* where the type of expr was a tuple-like object, whose elements would be bound
* to the variables x, y, and z (which this construct declares). Tuple-like objects
* include std::tuple, std::pair, std::array, and aggregate structures.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
using Coordinate = std::pair<int, float>;
Coordinate origin()
{
return Coordinate {7, 13.5};
}
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
{
const auto &[x, y] = origin();
std::cout << STD_TRACE_VAR2(x, y) << "\n" << std::endl;
}
{
const std::map<int, char> m {{1, 'a'}, {2, 'b'}};
for (const auto &[key, value] : m) {
std::cout << STD_TRACE_VAR2(key, value) << std::endl;
}
}
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
x: 7, y: 13.5
key: 1, value: a
key: 2, value: b
#endif