当非静态函数声明跟在静态函数声明之后会发生什么? [英] What happens when non-static function declaration follows static function declaration?

查看:145
本文介绍了当非静态函数声明跟在静态函数声明之后会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下编译:

static int foo() { return 1; }
int foo();

但是,它将始终编译吗?在这种情况下,行为是否定义明确?当非静态原型遵循静态声明时,这意味着什么?

But, will it always compile? Is the behavior in this case well defined? And what does it means when a non-static prototype follows a static declaration?

推荐答案

是的,它将进行编译并且行为已明确定义.由于fooint foo(); 1 之前声明为static,因此foo具有内部链接.

Yes it will compile and behavior is well defined. Since foo is declared static earlier than int foo();1, foo has internal linkage.

对于在可见该标识符的先前声明的范围内用存储类说明符extern声明的标识符, 31) (如果该先前声明指定了内部或外部)关联,则后面的声明中标识符的链接与先前声明中指定的链接相同. [...]

For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible,31) if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. [...]

和脚注指出:

31)按照6.2.1的规定,后面的声明可能会隐藏前面的声明.

31) As specified in 6.2.1, the later declaration might hide the prior declaration.


1 .如果未指定存储类别,则假定该函数具有外部链接.标准说:如果函数标识符的声明没有存储类说明符,则其链接 完全确定是否与存储类说明符extern-6.2.2(p5)声明的一样.


1. If no storage class is specified , the function is assumed to have external linkage. Standard says: If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern -- 6.2.2 (p5).

这篇关于当非静态函数声明跟在静态函数声明之后会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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