Skip to content

Commit 78c3d64

Browse files
committed
Typos in anonymous namespace section.
1 parent 16e918e commit 78c3d64

File tree

5 files changed

+39
-46
lines changed

5 files changed

+39
-46
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ endfunction()
5353

5454
add_example(
5555
ROOT anonymous-namespace
56-
FILES foo.cpp main.cpp
56+
FILES sound_system.cpp main.cpp
5757
STANDARD 98
5858
)
5959
add_example(
6060
ROOT anonymous-namespace
61-
FILES foo-anonymous.cpp main-anonymous.cpp
61+
FILES sound_system-anonymous.cpp main-anonymous.cpp
6262
STANDARD 11
6363
)
6464
add_example(

examples/anonymous-namespace/foo.cpp

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/anonymous-namespace/main-anonymous.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
namespace
44
{
5-
struct foo
5+
struct system
66
{
7-
foo(int i)
7+
system()
88
{
9-
printf("main.cpp %d\n", i);
9+
printf("main system\n");
1010
}
1111
};
1212
}
1313

14-
void build_foo(int i);
14+
void init_sound_system();
1515

16-
int main(int argc, char** argv)
16+
int main()
1717
{
18-
build_foo(argc);
19-
foo f(argc);
18+
init_sound_system();
19+
system main_system;
2020
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#include <cstdio>
22

3-
struct foo
3+
struct system
44
{
5-
foo(int i)
5+
system()
66
{
7-
printf("main.cpp %d\n", i);
7+
printf("main system\n");
88
}
99
};
1010

11-
void build_foo(int i);
11+
void init_sound_system();
1212

13-
int main(int argc, char** argv)
13+
int main()
1414
{
15-
build_foo(argc);
16-
foo f(argc);
15+
init_sound_system();
16+
system main_system;
1717
}

parts/11/anonymous-namespace.tex

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,48 @@ \subsection{Anonymous Namespaces}
1414

1515
However, this keyword is not applicable to type declarations. So what
1616
happened if we needed a class definition at file scope but happened to
17-
use the same name in different files, like with \code{struct foo}
17+
use the same name in different files, like with \code{struct system}
1818
below?
1919

20-
\lstinputlisting[title=foo.cpp]{examples/anonymous-namespace/foo.cpp}
20+
\lstinputlisting[title=sound\_system.cpp]
21+
{examples/anonymous-namespace/sound_system.cpp}
2122
\lstinputlisting[title=main.cpp]{examples/anonymous-namespace/main.cpp}
2223

2324
If we try to compile the these files for example with \code{g++
24-
foo.cpp main.cpp} then run the program, we would expect to have the
25-
following output:
25+
sound\_system.cpp main.cpp} then run the program, we would expect to
26+
have the following output:
2627

2728
\begin{lstlisting}{language=bash}
2829
$ ./a.out
29-
foo.cpp 1
30-
main.cpp 1
30+
sound system
31+
main system
3132
\end{lstlisting}
3233

3334
Instead, we may have the following output:
3435

3536
\begin{lstlisting}{language=bash}
3637
$ ./a.out
37-
foo.cpp 1
38-
foo.cpp 1
38+
sound system
39+
sound system
3940
\end{lstlisting}
4041

4142
I say ``may'' because this program violates the one definition rule,
4243
which states that a symbol cannot have more than one definition in the
43-
program. Here, however, the \code{struct foo} has two definitions, one
44-
in each cpp file. We are thus entering the territory of undefined
45-
behavior.
44+
program. Here, however, the \code{struct system} has two definitions,
45+
one in each cpp file. We are thus entering the territory of undefined
46+
behavior\footnote{An undefined behavior is the result of an ill-formed
47+
program, where the programmer wrote something explicitly forbidden by
48+
the standard. Typical examples of undefined behavior are: comparing
49+
iterators on different containers, overflowing a signed integer,
50+
creating a reference to nullptr, or defining two different types with
51+
the same name. The compiler can and does consider that such situations
52+
cannot occur, and will optimize the binary accordingly.}.
4653

4754
\bigskip
4855

49-
\Cpp11{} introduces the anonymous namespaces, which can totally solve
50-
this issue. Any symbol defined in an anonymous namespace has internal
51-
linkage. That means that it cannot conflict with anything defined
56+
With the anonymous namespaces from \cpp11, we can totally solve this
57+
issue. Any symbol defined in an anonymous namespace has internal
58+
linkage; which means that it cannot conflict with anything defined
5259
outside the current compilation unit. Its usage is as we would expect:
5360

5461
\begin{lstlisting}
@@ -64,10 +71,10 @@ \subsection{Anonymous Namespaces}
6471
is well defined and the output matches the expectations.
6572

6673
\lstinputlisting[
67-
title=foo-anonymous.cpp,
74+
title=sound\_system-anonymous.cpp,
6875
emph=namespace
6976
]{%
70-
examples/anonymous-namespace/foo-anonymous.cpp%
77+
examples/anonymous-namespace/sound_system-anonymous.cpp%
7178
}
7279

7380
\lstinputlisting[

0 commit comments

Comments
 (0)