为什么匿名命名空间中的函数/对象有外部链接? [英] Why do functions/objects inside anonymous namespace have external linkage?
问题描述
为什么在匿名命名空间中定义的符号(函数和变量)不具有static关键字的内部链接?
Why don't symbols (functions and variables) that are defined in an anonymous namespace have internal linkage as with static keyword? If a function is not visible/accessible outside, what is the reason to have external linkage?
推荐答案
在C ++ 03中,如果一个函数在外部不可见/具有内部链接的名称被禁止用作模板参数[*]。因此,未命名的命名空间中的大多数事物的名称具有外部链接,以允许它们与模板一起使用。您可以通过声明 static
在命名或全局命名空间中,在未命名的命名空间中显式地给出一个名称内部链接。
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.
C ++ 11中的改变 - 未命名命名空间中的名称默认为内部链接(3.5 / 4),内部链接的名称可以用作模板参数。
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.
[*]类型,它必须有外部链接。对于对象和函数,如果其地址用作模板参数,则它必须具有外部链接,但是例如可以使用const的值作为模板参数带内部链接的整数。
[*] 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.
这篇关于为什么匿名命名空间中的函数/对象有外部链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!