zoukankan      html  css  js  c++  java
  • C++ 匿名namespace的作用以及与static的区别

    匿名namespace的作用以及它与static的区别

    一。匿名namespace的作用
    在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做
    为函数名或者全局变量名,则在链接阶段就会发生重定义错误,为了解决这个
    问题,我们可以在定义这些标识符(identifier)的时候加上static关键字修
    饰以限制它只在一个tu范围内可见。
    C++继承了C语言中static关键字的这个用途,我们依旧可以使用static来避免
    多个tu中使用同一个标识符带来的重定义问题。此外C++还提供了另一种特有
    的方式,那就是匿名namespace:一个没有指定名字的namespace被称为一个匿
    名namespace;在一个tu中可以出现多个匿名namespace,并且相同层次的匿名
    namespace实际上被合成为同一个;出现在不同tu的匿名namespace中的相同标
    识符相互独立不会发生冲突,因此我们可以把那些只希望在同一个tu范围可见
    的全局标识符放入一个匿名namespace中,效果与前面加static相同。

    二。匿名namespace与static的区别
    一个全局标识符被static修饰后它的linkage变为internal linkage,这就是
    为什么不同tu中的相同标识符不会发生冲突的原因。
    而匿名namespace却并不会改变在它内部定义的标识符的linkage,它用来避免
    名字冲突所采用的手段同C++用来实现重载的手段一摸一样,就是使用名字改
    编(name mangling):根据C++标准7.3.1.1,每个tu中的匿名namespace实际
    上会拥有一个独一无二的名字,因此在不同tu的匿名namespace中相同的标识
    符实际上属于不同的namespace,自然在名字改编后就不会发生冲突了:

    7.3.1.1 Unnamed namespaces [namespace.unnamed]
    An unnamed-namespace-definition behaves as if it were replaced by
    namespace unique { /* empty body */ }
    using namespace unique;
    namespace unique { namespace-body }
    where all occurrences of unique in a translation unit are replaced
    by the same identifier and this identifier differs from all other
    identifiers in the entire program.

    为什么匿名namespace不采取跟static一样的做法呢,搞个新花样岂不是增加
    了编译器开发的负担?这其实是因为另一个C++的特性牵制了匿名namespace的
    实现,那就是模板非类型参数(template non-type arguments):

    14.3.2 Template non-type arguments [temp.arg.nontype]
    A template-argument for a non-type, non-template template-parameter
    shall be one of:
    — an integral constant-expression of integral or enumeration type; or
    — the name of a non-type template-parameter; or
    — the address of an object or function with external linkage, including
    function templates and function template-ids but excluding non-static
    class members, expressed as & id-expression where the & is optional
    if the name refers to a function or array, or if the corresponding
    template-parameter is a reference; or
    — a pointer to member expressed as described in 5.3.1 .

    正是被红字标出的external linkage这一需求限制了匿名namespace的实现!
    试想一下,假如我们有一个全局对象或者函数只希望它在一个tu中有效,又
    希望能够用它的地址来实例化一个模板,怎么办?只在一个tu中有效,可以
    选择internal linkage,但是要用它的地址做为模板参数,又要求它必须要
    是external linkage!!
    很显然,匿名namespace不改变其内部标识符的linkage这一性质解决了这一
    难题,我们可以把这个全局对象或者函数放心的扔在一个匿名namespace中,
    然后用它的地址来实例化一个模板,绝对不会发生重定义错误:)

    现在大部分C++书籍都认为匿名namespace和static是相同的,而正如这里所阐
    述的,它们之间差异是明显的:static修饰的标识符由于internal linkage的
    限制,是不能用来实例化模板的!

    最后给出一个例子证实匿名namespace确实不改变linkage,呵呵
    代码中验证了external linkage/internal linkage/no linkage三种情况
    ———————————————————

    template
    struct foo
    {
    void bar();
    };

    static char a =’a’;

    namespace
    {
    char b = ‘b’;
    static char c = ‘c’;

    template struct xxx {};

    void foobar()
    {
    struct no_linkage {};
    xxx<no_linkage>(); // 如果编译错误,说明no_linkage的linkage没有变化
    }
    }

    int main()
    {
    foo<&a>().bar(); // 由于a的linkage是internal,因此应该编译错误
    foo<&b>().bar(); // 如果编译正确,说明b的linkage是external
    foo<&c>().bar(); // 如果编译错误,说明c的linkage是internal

    foobar();

    return 0;
    }

    ———————————————————
    Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2003 Comeau Computing. All rights reserved.
    MODE:strict errors C++

    “ComeauTest.c”, line 19: error: a template argument may not reference a
    local type
    xxx<no_linkage>();
    ^
    ^

    “ComeauTest.c”, line 25: error: a template argument may not reference a
    non-external entity
    Hint: http://www.comeaucomputing.com/techtalk/templates/#stringliteral
    foo<&a>().bar();
    ^

    “ComeauTest.c”, line 27: error: a template argument may not reference a
    non-external entity
    Hint: http://www.comeaucomputing.com/techtalk/templates/#stringliteral
    foo<&c>().bar();
    ^

    3 errors detected in the compilation of “ComeauTest.c”.

  • 相关阅读:
    (五)SpringCloud学习系列-构建部门微服务消费者Module
    (四)SpringCloud学习系列-构建部门微服务提供者Module
    (三)SpringCloud学习系列-Rest微服务构建
    git提交 显示作者名不是自己
    linux一些常见命令
    支付宝退款demo
    ffmpeg截取视频
    excel导入数据到mysql
    二分法与冒泡排序
    mysql的级联删除
  • 原文地址:https://www.cnblogs.com/alantu2018/p/8988324.html
Copyright © 2011-2022 走看看