简体   繁体   English

常规命名空间内匿名命名空间内符号的链接

[英]Linkage of symbols within anonymous namespace within a regular namespace

In C++, putting a function or a variable in an anonymous namespace makes its linkage internal, ie the same as declaring it static on a file-level, but idiomatic C++. 在C ++中,将函数或变量放在匿名命名空间中会使其内部链接,即与在文件级别上声明static但是惯用的C ++相同。

What about an anonymous namespace within a normal namespace? 普通命名空间中的匿名命名空间怎么样? Does it still guarantee internal linkage? 它仍然保证内部联系吗?

// foo.cpp

void func1() {
    // external linkage
}

static void func2() {
    // internal linkage
}

namespace {
    void func3() {
        // internal linkage
    }
}

namespace ns1 {
    void func4() {
        // external linkage
    }

    namespace {
        void func3() {
            // still internal linkage?
        }
    }
}

It's not necessarily the case that entities in an anonymous namespace have internal linkage; 匿名命名空间中的实体不一定具有内部链接; they may actually have external linkage. 他们实际上可能有外部联系。

Since the unnamed namespace has a name that is unique to the translation unit in which it was compiled, you just can't refer to the entities declared in it from outside of that translation unit, regardless of what their linkage is. 由于未命名的命名空间的名称对于编译它的转换单元是唯一的,因此无论它们的链接是什么,您都无法从该转换单元外部引用在其中声明的实体。

The C++ standard says (C++03 7.3.1.1/note 82): C ++标准说(C ++ 03 7.3.1.1/note 82):

Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit. 虽然未命名的命名空间中的实体可能具有外部链接,但它们实际上由其翻译单元唯一的名称限定,因此永远不会从任何其他翻译单元中看到。

C++11 (draft N3337) §3.5/4: (emphasis mine) C ++ 11(草案N3337)§3.5/ 4 :(强调我的)

An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage . 未命名的命名空间或在未命名的命名空间中直接或间接声明的命名空间具有内部链接 All other namespaces have external linkage. 所有其他名称空间都有外部链接。 A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of 具有名称空间作用域的名称上面没有给出内部链接,如果是名称,则具有与封闭名称空间相同的链接

— a variable; - 一个变量; or 要么

— a function; - 一个功能; or 要么

— a named class (Clause 9), or an unnamed class defined in a typedef declaration in which the class has the typedef name for linkage purposes (7.1.3); - 命名类(第9节),或在typedef声明中定义的未命名类,其中类具有用于链接目的的typedef名称(7.1.3); or 要么

— a named enumeration (7.2), or an unnamed enumeration defined in a typedef declaration in which the enumeration has the typedef name for linkage purposes (7.1.3); - 命名枚举(7.2),或在typedef声明中定义的未命名枚举,其中枚举具有用于链接目的的typedef名称(7.1.3); or 要么

— an enumerator belonging to an enumeration with linkage; - 属于具有链接的枚举的枚举器; or 要么

— a template. - 一个模板。

This guarentees that any unnamed namespace has internal linkage. 这保证了任何未命名的命名空间都具有内部链接。

What about an anonymous namespace within a normal namespace? 普通命名空间中的匿名命名空间怎么样? Does it still guarantee internal linkage? 它仍然保证内部联系吗?

Although within a named (normal) namespace, it's an unnamed (anonymous) namespace and thus is guaranteed to have internal linkage as per the C++11 standard. 虽然在命名(普通)命名空间内,它是一个未命名的(匿名)命名空间,因此保证按照C ++ 11标准具有内部链接。


putting a function or a variable in an anonymous namespace makes its linkage internal, ie the same as declaring it static on a file-level, but idiomatic C++. 将函数或变量放在匿名命名空间中会使其内部链接,即与在文件级别上声明静态但是惯用的C ++相同。

In C++11 the usage of static in this context was undeprecated ; 在C ++ 11中, static在这种情况下的使用尚未得到证实 ; although unnamed namespace is a superior alternative to static , there're instances where it fails which is remedied by static ; 尽管未命名的命名空间是static一种优越的替代方法但它存在失败的情况,这可以通过static来补救; inline namespace was introduced in C++11 to address this. 在C ++ 11中引入了inline namespace来解决这个问题。

$3.5/3 - "A name having namespace scope (3.3.6) has internal linkage if it is the name of $ 3.5 / 3 - “具有命名空间范围(3.3.6)的名称具有内部链接(如果它的名称)

— a variable, function or function template that is explicitly declared static; - 显式声明为static的变量,函数或函数模板; or, 要么,

— a variable that is explicitly declared const and neither explicitly declared extern nor previously declared to have external linkage; - 一个显式声明为const的变量,既未显式声明为extern,也未声明为具有外部链接; or 要么

— a data member of an anonymous union. - 匿名联盟的数据成员。

So, I doubt if any of the names 'func3' and 'func4' in your program have internal linkage at all. 所以,我怀疑程序中的任何名称'func3'和'func4'是否都有内部链接。 They have external linkage. 他们有外部联系。 However, it is just that they can not be referred from other translation units in accordance with the quote from James. 但是,只是根据詹姆斯的引用,他们不能从其他翻译单位转介。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM