-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinitializerList.cpp
51 lines (35 loc) · 1002 Bytes
/
initializerList.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
#include <initializer_list>
#include <iostream>
#include <string>
class MyData{
public:
MyData(std::string, int){
std::cout << "MyData(std::string, int)" << '\n';
}
MyData(int, int){
std::cout << "MyData(int, int)" << '\n';
}
MyData(std::initializer_list<int>){
std::cout << "MyData(std::initializer_list<int>)" << '\n';
}
};
template<typename T>
void printInitializerList(std::initializer_list<T> inList){
for (auto& e: inList) std::cout << e << " ";
}
int main(){
std::cout << '\n';
// sequence constructor has a higher priority
MyData{1, 2};
// invoke the classical constructor explicitly
MyData(1, 2);
// use the classical constructor
MyData{"dummy", 2};
std::cout << '\n';
// print the initializer list of ints
printInitializerList({1, 2, 3, 4, 5, 6, 7, 8, 9});
std::cout << '\n';
// print the initializer list of strings
printInitializerList({"Only", "for", "testing", "purpose."});
std::cout << "\n\n";
}