Skip to content

Commit 1a8883b

Browse files
committed
Concept docs for "Namespaces" (closes #605)
1 parent d512108 commit 1a8883b

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"blurb": "Code can be organized in namespaces for a clearer structure.",
3+
"authors": [
4+
"vaeng"
5+
],
6+
"contributors": [
7+
]
8+
}

concepts/namespaces/about.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# About
2+
3+
An important method for code organization are namespaces.
4+
Two functions might have a naming collision, which can be resolved by putting them in different namespaces.
5+
Namespaces can be nested, which might help to structure big code bases.
6+
Access to the namespaces is done via the scope-resolution operator `::`.
7+
8+
The example below shows the use of two different `foo` functions.
9+
They are used together by prefixing their respective namespaces.
10+
11+
```cpp
12+
namespace my_ns {
13+
int foo() {
14+
return 44;
15+
}
16+
namespace my_inner_ns {
17+
int baz() {
18+
return 90;
19+
}
20+
}
21+
}
22+
namespace my_other_ns {
23+
int foo() {
24+
return -2;
25+
}
26+
}
27+
int myresult{my_ns::foo() + my_other_ns::foo() * my_ns::my_inner_ns::baz()};
28+
```
29+
30+
~~~~exercism/advanced
31+
Deeply nested namespaces might be too verbose.
32+
It is possible to remove the verbosity with a namespace import via `using`.
33+
This moves all names into the global namespace.
34+
To keep some differentiation aliases might be useful.
35+
36+
```cpp
37+
int my_other_result{my_ns::my_inner_ns::baz()};
38+
39+
using namespace my_ns::my_inner_ns; // importing the complete namespace
40+
int also_other_result{baz()};
41+
42+
namespace m = my_ns; // setting an alias
43+
namespace o = my_other_ns;
44+
45+
int also_my_result{m::foo() + o::foo() * m::my_inner_ns::baz()};
46+
```
47+
~~~~
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Introduction
2+
3+
An important method for code organization are namespaces.
4+
Two functions might have a naming collision, which can be resolved by putting them in different namespaces.
5+
Namespaces can be nested, which might help to structure big code bases.
6+
Access to the namespaces is done via the scope-resolution operator `::`.
7+
8+
The example below shows the use of two different `foo` functions.
9+
They are used together by prefixing their respective namespaces.
10+
11+
```cpp
12+
namespace my_ns {
13+
int foo() {
14+
return 44;
15+
}
16+
namespace my_inner_ns {
17+
int baz() {
18+
return 90;
19+
}
20+
}
21+
}
22+
namespace my_other_ns {
23+
int foo() {
24+
return -2;
25+
}
26+
}
27+
28+
int myresult{my_ns::foo() + my_other_ns::foo() * my_ns::my_inner_ns::baz()};
29+
```

concepts/namespaces/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"url": "https://en.cppreference.com/w/cpp/language/namespace",
4+
"description": "C++ reference on namespaces"
5+
}
6+
]

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,11 @@
827827
"uuid": "e52453a5-4997-4eba-b754-5bda7b97c701",
828828
"slug": "basics",
829829
"name": "Basics"
830+
},
831+
{
832+
"uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521",
833+
"slug": "namespaces",
834+
"name": "Namespaces"
830835
}
831836
],
832837
"key_features": [

0 commit comments

Comments
 (0)