@@ -14,41 +14,48 @@ \subsection{Anonymous Namespaces}
1414
1515However, this keyword is not applicable to type declarations. So what
1616happened 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 }
1818below?
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
2324If 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
3334Instead, 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
4142I say `` may'' because this program violates the one definition rule,
4243which 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- \Cpp 11{} 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 \cpp 11, 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
5259outside the current compilation unit. Its usage is as we would expect:
5360
5461\ begin{lstlisting}
@@ -64,10 +71,10 @@ \subsection{Anonymous Namespaces}
6471is 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