Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Hyphens for everyone!

...

Production-quality C++ code frequently uses header files as a means to share code between translation units. A header file is any file that is inserted into a translation unit through a #include directive. Do not define an unnamed namespace in a header file. When an unnamed namespace is defined in a header file, it can lead to surprising results. Due to default internal linkage, each translation unit will define its own unique instance of members of the unnamed namespace that are ODR-used within that translation unit. This can cause unexpected results, bloat the resulting executable, or inadvertently trigger undefined behavior due to One Definition Rule one-definition rule (ODR) violations.

Page properties
hiddentrue

This rule could be argued to be more of a style recommendation than a rule. However, this is prevalent advice given by many sources, such as Google's coding style guidelines (http://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Namespaces), and the behavior is subtle-but-surprising enough that it will trip up programmers. The most common rationale behind why unnamed namespaces are bad in a header file has to do with ODR violations, but from what I can research, the unnamed namespace in a header isn't to blame for those violations per-se, it's misunderstanding what an unnamed namespace does in conjunction with some other language feature, like inline functions.

I am writing this to be a rule, but we'll see what kind of push-back happens in practice as to whether it remains a rule, or gets demoted to a strong recommendation.

...

In this noncompliant code example, the variable v is defined in an unnamed namespace within a header file, and an inline function, get_v(), is defined, which accesses that variable. ODR-using the inline function from multiple translation units (as shown in the implementation of f() and g()) violates the One Definition Rule one-definition rule because the definition of get_v() is not identical in all translation units (due to referencing a unique v in each translation unit).

...

See MSC52-CPP. Obey the one-definition rule for more information on violations of the One Definition Ruleone-definition rule.

Compliant Solution

In this compliant solution, v is defined in only one translation unit but is externally visible to all translation units and can be accessed from the inline get_v() function:

...

In this noncompliant code example, the function f() is defined within a header file. However, including the header file in multiple translation units causes a violation of the One Definition Rule one-definition rule that usually results in an error diagnostic generated at link time due to multiple definitions of a function with the same name.

...

Defining an unnamed namespace within a header file can cause data integrity violations and performance problems but is unlikely to go unnoticed with sufficient testing. One Definition Rule -definition rule violations result in undefined behavior.

...