在常规命名空间内的匿名命名空间内的符号链接 [英] Linkage of symbols within anonymous namespace within a regular namespace

查看:305
本文介绍了在常规命名空间内的匿名命名空间内的符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,将函数或变量放在匿名命名空间中使得其链接内部, e。与在文件级别但是惯用的C ++上声明 static 一样。



匿名命名空间一个正常的命名空间?它仍然保证内部链接?

  // foo.cpp 

void func1 b $ b //外部链接
}

static void func2(){
//内部链接
}

namespace {
void func3(){
//内部链接
}
}

命名空间ns1 {
void func4(){
//外部链接
}

命名空间{
void func3(){
//内部链接?
}
}
}


解决方案>

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



未命名的命名空间或在未命名的命名空间内直接或间接声明的命名空间具有内部链接。所有其他命名空间具有外部链接。如果名称为



- 一个变量,具有上面没有给出内部链接的命名空间范围的名称具有与包围命名空间相同的链接;或



- 一个函数;或



- 一个命名类(第9条)或在typedef声明中定义的未命名类,其中类具有用于链接目的的typedef名称(7.1.3) ;或



- 一个命名枚举(7.2)或在typedef声明中定义的未命名枚举,其中枚举具有用于链接目的的typedef名称(7.1.3);或



- 属于具有连接的枚举的枚举器;或



- 一个范本。




在正常命名空间中的匿名命名空间怎么办?它仍然保证内部链接?


虽然在一个命名的(正常的)命名空间内,它是一个未命名(匿名)命名空间,具有根据C ++ 11标准的内部链接。







放置函数或匿名命名空间中的变量使其链接内部,i。 e。与在文件级声明它是静态的相同,但是惯用的C ++。


在C ++ 11中使用 static 在此上下文中已取消淘汰 ;虽然未命名的命名空间是 static 的优越替代方法,有一些情况下,它失败,由 static 修复; inline命名空间是在C ++ 11中引入的


In C++, putting a function or a variable in an anonymous namespace makes its linkage internal, i. e. the same as declaring it static on a file-level, but idiomatic 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?
        }
    }
}

解决方案

C++11 (draft N3337) §3.5/4: (emphasis mine)

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); 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); 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.


putting a function or a variable in an anonymous namespace makes its linkage internal, i. e. the same as declaring it static on a file-level, but idiomatic C++.

In C++11 the usage of static in this context was undeprecated; although unnamed namespace is a superior alternative to static, there're instances where it fails which is remedied by static; inline namespace was introduced in C++11 to address this.

这篇关于在常规命名空间内的匿名命名空间内的符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆