根据C ++ 17标准草案,在块范围内声明为“ extern”的函数的链接 [英] Linkage of function declared as `extern` in block scope according to the C++17 standard draft

查看:109
本文介绍了根据C ++ 17标准草案,在块范围内声明为“ extern”的函数的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 C ++ 17标准草案 §3.5.6


函数名称在块作用域中声明的变量和在块作用域中声明的变量的名称extern
声明具有链接。 如果存在具有相同名称和
类型链接的实体的可见声明
,则忽略在最内层封闭的命名空间范围之外声明的实体,块范围声明
声明同一实体,并接收先前声明的链接。如果有多个这样的
匹配实体,则该程序格式不正确
。否则,如果找不到匹配的实体,则块作用域实体
会接收外部链接

The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity, the program is ill-formed. Otherwise, if no matching entity is found, the block scope entity receives external linkage

此外,在标准:

static void f();
static int i = 0;              // #1
void g() {
    extern void f();          // internal linkage
    int i;                    // #2 i has no linkage
    {
        extern void f();      // internal linkage
        extern int i;         // #3 external linkage
    }
}

执行第三个函数声明 extern void f()收到:

Does the third function declaration extern void f() receives:


  • external 链接(由于前面的 extern void f()声明)

  • 内部链接(由于上述块范围内的第二个声明 f()从声明 f( ) :: 全局名称空间中)

  • 还是格式错误,因为还有更多的不止一个这样的匹配
    实体?

  • external linkage ( due to the preceding extern void f() declaration )
  • internal linkage ( since the second declaration of f() in the block scope above receives internal linkage from the declaration of f()in the :: global namespace )
  • Or is it ill-formed because there are more " more than one such matching entity" ?

编辑

main.cpp

#include <iostream>

extern void g();
void f() { std::cout << "main f() called" << std::endl; }

int main(){
    g(); 
    return 0; 
}  

test.cpp

#include <iostream>

static void f() { std::cout << " test f() called" << std::endl; }
void g() {
    extern void f(); 
    {
        extern void f(); 
        f(); 
    }
}

g() main.cpp main()函数中调用。

使用 gcc 5.4.0


g ++ -std = c ++ 14 -wall main.cpp test.cpp

g++ -std=c++14 -Wall main.cpp test.cpp

打印:


main f()被称为

main f() called

因此,gcc显然会处理 f()的调用 g()中的具有 external 链接(调用<$中提供的定义c $ c> main.cpp 文件)。标准示例中的注释是错误的(#3 的函数声明没有内部链接),或者是来自 gcc <的编译器错误。 / code>。

Thus, gcc obviously treats the call of f() from within g() with external linkage ( calling the definition provided in the main.cpp file ). Either the comment in the example from the standard is wrong ( the function declaration of #3 has not interal linkage ) or its a compiler bug from gcc.

推荐答案

我不明白您的困惑。最封闭的名称空间范围是全局名称空间的范围。第一个和第二个声明的类型和名称匹配,因此外部块作用域声明仅继承了全局声明的链接(由于 static 说明符而处于内部)。第二个区块范围声明与第一个声明关联;你明白了。

I don't understand your confusion. The innermost enclosing namespace scope is that of the global namespace; the first and second declarations match in type and name, and hence the outer block-scope declaration just inherits the global declaration's linkage (which is internal due to the static specifier). The second block-scope declaration is associated with the first one; you get the idea.

这篇关于根据C ++ 17标准草案,在块范围内声明为“ extern”的函数的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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