-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNamedParameterByStruct.cpp
84 lines (72 loc) · 1.88 KB
/
NamedParameterByStruct.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* \file NamedParameterByStruct.cpp
* \brief Solve order of the parameters problem
*
* Pass params as sruct
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
// Named Params - No
void draw(size_t xPosition, size_t yPosition, size_t width, size_t height, bool drawingNow)
{
std::cout
<< "Named Params - No:\n"
<< STD_TRACE_VAR5(xPosition, yPosition, width, height, drawingNow)
<< "\n" << std::endl;
}
//--------------------------------------------------------------------------------------------------
// Named Params - Yes
struct Params
{
int xPosition;
int yPosition;
int width;
int height;
bool drawingNow;
};
void draw(const Params ¶ms)
{
std::cout
<< "Named Params - Yes:\n"
<< STD_TRACE_VAR5(params.xPosition, params.yPosition, params.width, params.height,
params.drawingNow)
<< "\n" << std::endl;
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
const auto p1 {20};
const auto p2 {50};
const auto p3 {100};
const auto p4 {5};
const auto p5 {true};
// Named Params - No
{
::draw(p1, p2, p3, p4, p5);
}
// Named Params - Yes
{
// error: C++ designated initializers only available with -std=c++2a or -std=gnu++2a
#if 0
const Params params
{
.xPosition = p1,
.yPosition = p2,
.width = p3,
.height = p4,
.drawingNow = p5
};
::draw(params);
#endif
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
Named Params - No:
xPosition: 20, yPosition: 50, width: 100, height: 5, drawingNow: 1
Named Params - Yes:
params.xPosition: 20, params.yPosition: 50, params.width: 100, params.height: 5, params.drawingNow: 1
#endif