简体   繁体   English

如何从另一个命名空间而不是全局命名空间定义函数和数据?

[英]How to define functions and data from another namespace, not in the global one?

Like if I have a header with some declarations: 就像我有一些声明的标头一样:

"Header.hpp" “Header.hpp”

extern int SomeData;

int SomeFunc();

And an Implementation of it (in which I declare all the functions and global variables in an unnamed namespace in order to avoid linkage problems): 以及它的实现(为了避免链接问题,我在一个未命名的命名空间中声明了所有函数和全局变量):

"Header.cpp" “Header.cpp”

#include "Header.hpp"

inline namespace
{
    const int SomeLocalFileData = 9;

    struct MyLocalStructure;

    int SomeLocalFunction(MyLocalStructure *);

    int ::SomeData(SomeLocalFileData);

    int ::SomeFunc()
    {
        return SomeLocalFunction(nullptr);
    }
}

The above code will fail with the following errors (using 'gcc' compiler): 上面的代码将失败,并出现以下错误(使用“ gcc”编译器):

error: declaration of 'SomeData' not in a namespace surrounding '::' int ::SomeData(SomeLocalFileData); 错误:声明'SomeData'不在围绕'::'的命名空间中int :: SomeData(SomeLocalFileData);

error: declaration of 'int SomeFunc()' not in a namespace surrounding '::' int ::SomeFunc() 错误:不在周围::: int :: SomeFunc()的命名空间中声明了'int SomeFunc()'

So Is there anyway I can declare all my local file symbols to be in an anonymous namespace, in order to avoid linkage problems but in the same time to be able to define my function and data exports. 因此,无论如何,我可以将所有本地文件符号声明为匿名名称空间,以避免链接问题,但同时又可以定义函数和数据导出。

You are not allowed to define object of the global scope in your anonymous namespace. 不允许在匿名名称空间中定义全局范围的对象。

If you want to share SomeData and SomeFunc() with other compilation units, you simply have to define them outside your namespace. 如果要与其他编译单元共享SomeDataSomeFunc() ,只需在名称空间之外定义它们。

The anonymous namespace is meant to avoid your symbols to be shared outside their compilation unit. 匿名名称空间是为了避免符号在其编译单元之外共享。 So if you don't want to share the symbols, define them in the anaonymous namespace. 因此,如果您不想共享符号,请在匿名名称空间中定义它们。 Declaring them in a header will then not make them available to other compilation units. 然后在标头中声明它们将不会使它们可用于其他编译单元。 If you watt to keep the definition in a header that is only included in this file, you could enclose the definition in an anonymous namespace in the same way. 如果您将定义保留在仅包含在此文件中的标头中,则可以用相同的方式将定义括在匿名名称空间中。

I believe what you are trying to do in your Header.cpp file is: 我相信您要在Header.cpp文件中尝试执行的操作是:

inline namespace
{
  const int SomeLocalFileData = 9;

  struct MyLocalStructure;

  int SomeLocalFunction(MyLocalStructure *);
}

int SomeData(SomeLocalFileData);

int SomeFunc()
{
    return SomeLocalFunction(nullptr);
}

Being in same translation unit and after the anonymous namespace, the definitions of SomeData and SomeFunc have access to the local symbols (eg SomeLocalFileData , MyLocalStructure and SomeLocalFunction ) defined in that anonymous namespace. 在同一个转换单元中,并且在匿名名称空间之后, SomeDataSomeFunc的定义可以访问在该匿名名称空间中定义的本地符号(例如SomeLocalFileDataMyLocalStructureSomeLocalFunction )。 Those symbols will remain hidden from other translation units. 这些符号将对其他翻译单元保持隐藏。

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

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