外部“C"C++ 命名空间内的链接? [英] extern "C" linkage inside C++ namespace?

查看:25
本文介绍了外部“C"C++ 命名空间内的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace someNameSpace {
    extern "C" void doSomething()
        {
             someOperations();
        }
}

我想在 C++ 和 C 环境中运行 doSomething().

I want to run doSomething() in both C++ and C environment.

如果我将 someNameSpace 暴露给 extern "C" 链接,它是否仍在封装 doSomething()?

Is someNameSpace still encapsulating doSomething() if I expose it to extern "C" linkage?

有没有一种好方法可以在 C++ 和 C 之间共享函数,同时避免污染 C++ 端的全局命名空间?

Is there a good way to share functions between C++ and C while avoiding polluting global namespace on C++ side?

编辑:因为这段代码主要用于C++模式,而C链接仅供测试使用,我想这是一个更好的方法.

Edit: Because this code is primarily used in C++ mode, while the C linkage is for test use only, I guess this is a better way to do it.

namespace someNameSpace {
    #ifdef COMPILE_FOR_C_LINKAGE
    extern "C"
    #else
    extern "C++"
    #endif
    { 
        void doSomething()
            {
                 someOperations();
            }
    }
}

推荐答案

您的代码可以工作,但您应该注意所有具有 extern "C" 链接的函数共享相同的名称空间,但是不要与命名空间"的 C++ 概念混淆:您的函数实际上是 someNameSpace::doSomething,但您不能使用任何其他 extern "C" 函数任何其他命名空间中的非限定名称 doSomething.

Your code works, but you should beware that all functions that have extern "C" linkage share the same space of names, but that is not to be confused with the C++ notion of "namespace": Your function is really someNameSpace::doSomething, but you cannot have any other extern "C" function with unqualified name doSomething in any other namespace.

参见 7.5/6:

至多一个具有特定名称的函数可以具有 C 语言链接.函数的两个声明与具有相同函数名称(忽略限定它的命名空间名称)的 C 语言链接出现在不同的命名空间作用域指的是同一个函数.C 变量的两个声明具有相同名称的语言链接(忽略限定它的命名空间名称)出现在不同的命名空间范围指的是同一个变量.具有 C 语言链接的实体不得声明为与全局范围内的变量同名,除非两个声明都表示同一个实体;没有诊断是如果声明出现在不同的翻译单元中,则需要.具有 C 语言链接的变量不应与具有 C 语言链接的函数同名声明(忽略命名空间名称限定各自的名称);如果声明出现在不同的翻译中,则不需要诊断单位.[注意:对于具有给定名称和 C 语言链接的实体,只有一个定义可以出现在程序(见 3.2);这意味着这样一个实体不能在多个命名空间中定义范围.— 尾注]

At most one function with a particular name can have C language linkage. Two declarations for a function with C language linkage with the same function name (ignoring the namespace names that qualify it) that appear in different namespace scopes refer to the same function. Two declarations for a variable with C language linkage with the same name (ignoring the namespace names that qualify it) that appear in different namespace scopes refer to the same variable. An entity with C language linkage shall not be declared with the same name as a variable in global scope, unless both declarations denote the same entity; no diagnostic is required if the declarations appear in different translation units. A variable with C language linkage shall not be declared with the same name as a function with C language linkage (ignoring the namespace names that qualify the respective names); no diagnostic is required if the declarations appear in different translation units. [Note: Only one definition for an entity with a given name with C language linkage may appear in the program (see 3.2); this implies that such an entity must not be defined in more than one namespace scope. — end note]

贵公司或项目的全球风格仲裁员应该能够就适合您的代码库的命名策略向您提供建议.

Your company's or project's global style arbiters should be able to advise you on a suitable naming policy for your code base.

这篇关于外部“C"C++ 命名空间内的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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