-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy path2.21.inline.variable.cpp
More file actions
26 lines (22 loc) 路 702 Bytes
/
Copy path2.21.inline.variable.cpp
File metadata and controls
26 lines (22 loc) 路 702 Bytes
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
//
// 2.21.inline.variable.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
#include <iostream>
struct Widget {
// C++17: define and initialize a static data member inside the class
static inline int count = 0;
Widget() { ++count; }
};
// C++17: an inline variable can be safely defined in a header and
// included by multiple translation units without violating the ODR
inline int global_value = 42;
int main() {
Widget a, b, c;
std::cout << "Widget::count = " << Widget::count << std::endl; // 3
std::cout << "global_value = " << global_value << std::endl; // 42
}