forked from nesteruk/ModernCpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniformInit.cpp
88 lines (64 loc) · 1.4 KB
/
UniformInit.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
85
86
87
88
#include "Headers.h"
struct Address {
};
struct Person
{
string name;
int age;
Person(const string &name, int age) : name(name), age(age) { }
Address address;
Person(Address address) {}
};
struct PersonFactory
{
static Person make_person(string name, int age){
return {name, age};
}
};
struct Exchange
{
int count;
float rates[2];
// this won't work
//Exchange(initializer_list<float> r) : rates(r) {}
Exchange(std::initializer_list<float> r)
{
if (r.size() < 2) return;
auto i = r.begin(); // ---> segway into next segment
rates[0] = *i;
i++;
rates[1] = *i;
}
};
int main()
{
int a = 4;
int n{4};
// shouldn't work
int m{3.5};
cout << m << endl;
string s{"foo"};
// remark on the confusion between init lists and member init lists
vector<int> values{1,2,3}; // = optional
// values.push_back(1);
// values.push_back(1);
// values.push_back(1);
array<float,3> coeff{0.1,0.2,0.3};
vector<int> what_is_this{123};
// careful here
map<string,string> capitals = {
{"UK", "London"},
{"France", "Paris"}
};
// but it's not just the built-in types!
Person p2{"Dmitri", 500};
auto p = PersonFactory::make_person("Dmitri", 500);
cout << p.name << " " << p.age << endl;
// most vexing parse
//Person person(Address());
Person person{Address{}};
auto z = person.address;
// here's a more interesting case
Exchange e{1,2,3};
return 0; // this line is critical
}