嵌套的未命名名称空间? [英] Nested unnamed namespace?

查看:65
本文介绍了嵌套的未命名名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用未命名的名称空间时,如果它嵌套在另一个名称空间中,会有任何问题吗?例如,以下代码中的Foo1.cpp和Foo2.cpp之间有什么真正的区别:

When using an unnamed namespace are there any issues if it is nested within another namespace? For example, is there any real difference between Foo1.cpp and Foo2.cpp in the following code:

// Foo.h
namespace Foo
{
    void fooFunc();
}

// Foo1.cpp
namespace Foo
{
    namespace
    {
        void privateFunction()
        {
            ...
        }
    }

    void fooFunc()
    {
        privateFunction();
    }
}

// Foo2.cpp
namespace
{
    void privateFunction()
    {
        ...
    }
}

namespace Foo
{
    void fooFunc()
    {
        privateFunction();
    }
}

推荐答案

未命名的命名空间可以被视为具有唯一名称(您不知道)的普通命名空间.根据C ++标准7.3.1.1:

Unnamed namespace could be considered as a normal namespace with unique name which you do not know. According to C++ Standard 7.3.1.1:

未命名空间定义的行为就像被替换

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.

没有其他问题.

这篇关于嵌套的未命名名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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