简体   繁体   English

块内的静态函数声明

[英]static function declaration inside a block

A tutorial said 一个教程

You cannot declare functions within a block as static. 您不能将块中的函数声明为静态。

What is the reason?How does it affect the function? 是什么原因?它如何影响功能?

static when applied to functions means that the function has internal linkage . 应用于函数时static意味着该函数具有内部链接 ie it can be accessed within this file only. 即它只能在此文件中访问。 In other words it has file-scope . 换句话说,它具有文件范围 This linkage applies irrespective of whether you declare a function inside another function or outside of all of them. 无论您是在另一个函数内部还是在所有函数之外声明函数,此链接都适用。

Now, if you apply the static keyword to a function declaration inside another block/function, semantically speaking , you are trying to restrict the scope of the function to that particular block/function, which goes against the language rules that state that static functions, irrespective of their place of declaration, shall have file-scope. 现在,如果将static关键字应用于另一个块/函数内的函数声明 ,从语义上讲 ,您试图将函数的范围限制为该特定的块/函数,这违反了声明static函数的语言规则,不论其声明地点如何,均应有档案范围。

And hence, the standard might be prohibiting such a declaration. 因此,标准可能会禁止这样的声明。

PS : Footnote 30 in 6.2.3 in the latest C11 standard states- PS :最新C11标准6.2.3中的脚注30

A function declaration can contain the storage-class specifier static only if it is at file scope; 只有当函数声明位于文件范围时,它才能包含存储类说明符static ;

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

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