-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnested_namespaces.cpp
More file actions
53 lines (44 loc) · 1.97 KB
/
Copy pathnested_namespaces.cpp
File metadata and controls
53 lines (44 loc) · 1.97 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
53
// Demos for nested_unnamed_anonymous_namespaces.md
//
// The doc covers two topics:
// 1) Anonymous (unnamed) namespaces -> translation-unit-local (internal linkage).
// 2) Nested namespaces -> classic form vs. the C++17 A::B::C shorthand.
#include <iostream>
// ---------------------------------------------------------------------------
// 1) Anonymous namespace: identifiers are local to this translation unit.
// The doc also notes that you may extend an unnamed namespace within the
// same translation unit, so we split it into two blocks like the doc does.
// ---------------------------------------------------------------------------
namespace {
int variable = 42;
void funct(int); // declared here
} // namespace
namespace {
void funct(int i) { std::cout << "anonymous funct, i = " << i << "\n"; }
} // namespace
// ---------------------------------------------------------------------------
// 2a) Nested namespaces: classic (pre-C++17) form.
// Imagine a small robot stack: Robot -> Sensors -> Lidar.
// ---------------------------------------------------------------------------
namespace Robot {
namespace Sensors {
namespace Lidar {
void scan() { std::cout << "Robot::Sensors::Lidar::scan (classic form)\n"; }
} // namespace Lidar
} // namespace Sensors
} // namespace Robot
// ---------------------------------------------------------------------------
// 2b) Nested namespaces: C++17 shorthand A::B::C.
// ---------------------------------------------------------------------------
namespace Robot::Actuators::Motor {
void spin() { std::cout << "Robot::Actuators::Motor::spin (C++17 shorthand)\n"; }
} // namespace Robot::Actuators::Motor
int main() {
std::cout << "--- anonymous namespace ---\n";
funct(variable); // file-local function and variable
std::cout << "--- nested namespace (classic) ---\n";
Robot::Sensors::Lidar::scan();
std::cout << "--- nested namespace (C++17 shorthand) ---\n";
Robot::Actuators::Motor::spin();
return 0;
}