Why do functions/objects inside anonymous namespace have external linkage?

[*]

In C++03, names with internal linkage were forbidden from being used as template arguments[*]. So, names of most things in unnamed namespaces had external linkage to allow their use with templates. You could explicitly give a name internal linkage in an unnamed namespace by declaring it static, same as in a named or global namespace.

Both things changed in C++11 — names in unnamed namespaces have internal linkage by default (3.5/4), and names with internal linkage can be used as template arguments.

[*] for types, it must have external linkage. For objects and functions, it must have external linkage if its address is used as a template argument, although it’s OK for example to use as a template argument the value of a const integer with internal linkage.

Leave a Comment