forked from mcpp-community/d2mcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09-list-initialization-0.cpp
More file actions
52 lines (40 loc) · 1.12 KB
/
09-list-initialization-0.cpp
File metadata and controls
52 lines (40 loc) · 1.12 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
52
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: dslings/cpp11/09-list-initialization-0.cpp
//
// Exercise/练习: cpp11 | 09 - list initialization | 窄化检查
//
// Tips/提示: 根据编译器的输出, 修复编译器报错, 了解列表初始化的窄化检查
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/list_initialization.html
// - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp11/09-list-initialization.md
//
// Auto-Checker/自动检测命令:
//
// d2x checker list-initialization
//
#include <d2x/cpp/common.hpp>
#include <iostream>
int main() {
int a1 = 1.1;
d2x_assert_eq(a1, 1);
int a2 = 1.1;
int a3 = { 1.1 };
d2x_assert_eq(a2, 1);
d2x_assert_eq(a3, 1);
double b1 { 1.1 };
constexpr double c1 { 2.2 };
int b2 { b1 };
int c2 { c1 };
d2x_assert_eq(b2, 1);
d2x_assert_eq(c2, 2);
int arr1[] = { 1, 2.2, 3 };
d2x_assert_eq(arr1[1], 2);
int arr2[4] { 1, b1, c1 };
d2x_assert_eq(arr2[1], 1);
d2x_assert_eq(arr2[2], 2);
d2x_assert_eq(arr2[3], 0);
D2X_WAIT
return 0;
}