EXTERN" C" C ++命名空间内的联系? [英] extern "C" linkage inside C++ namespace?

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

问题描述

 命名空间someNameSpace {
    为externC无效DoSomething的()
        {
             someOperations();
        }
}

我要运行 DoSomething的()在C ++和C的环境。

someNameSpace 还是封装 DoSomething的()如果我将其置于为externC 联动?

是否有共享C的功能++和C同时避免对C ++侧污染全局命名空间的好办法?

修改:因为这个code是C ++模式主要用于,而C链接是测试用而已,我想这是一个更好的办法来做到这一点。

 命名空间someNameSpace {
    #IFDEF COMPILE_FOR_C_LINKAGE
    为externC
    #其他
    为externC ++
    #万一
    {
        无效DoSomething的()
            {
                 someOperations();
            }
    }
}


解决方案

您code的作品,但你应该提防有所有功能的externC联动共享名称相同的空间,但不与空间的C ++概念相混淆:您的功能实在是 someNameSpace :: DoSomething的,但你不能有任何其他的externC与不合格的名称 DoSomething的在任何其他命名空间的功能。

请参阅7.5 / 6:


  

目前最多只有一个具有特定名称的函数可以有C语言联动。两个声明一个函数
  与具有相同功能名称C语言链接(忽略限定它的命名空间名称)的
  出现在不同的空间范围指的是相同的功能。两个声明的用C变量
  语言联动具有相同的名称(忽略限定它的命名空间的名称),出现在不同的
  命名空间范围指的是相同的变量。用C语言联动的实体不得与声明
  相同的名称,在全球范围内的变量,除非两个声明表示相同的实体;没有诊断是
  如果声明出现在不同的翻译单元必需的。用C语言链接的变量不得
  用相同的名称,用C语言链接的函数声明(忽略命名空间名称的
  限定各自的名称);如果没有声明出现在不同的翻译诊断要求
  单位。 [注意的:与C语言的联系可能会出现在一个给定的名称的实体只有一个定义
  该方案(见3.2);这意味着,这样的实体不能在多个名称空间中定义
  范围。 - 注完的]


您的公司或项目的全局样式仲裁者应该能够建议您为您的code基合适的命名策略。

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

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

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

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

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();
            }
    }
}

解决方案

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.

See 7.5/6:

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.

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

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