为什么在函数参数中使用静态变量声明函数在Windows中不是错误? [英] Why declaring funciton with static variables in function arguments is not a error in Windows?

查看:898
本文介绍了为什么在函数参数中使用静态变量声明函数在Windows中不是错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关来自

的静态和其他说明符的文档。

这里,它表示


静态说明符只允许在变量声明(函数参数列表除外),函数声明(块范围除外)和匿名联合体的声明。当在类成员的声明中使用时,它声明一个静态成员。当在变量的声明中使用时,它指定静态存储持续时间(除非伴随着thread_local)。当在命名空间范围的声明中使用时,它指定内部链接。


现在考虑像

  int test(static int a)
{
return a;
}

int main()
{

test(5);
return 0;
}

这是standerd本身的完整发音。

此代码段在Windows和运行时编译时出现警告


C4042:'a' p>

但在Linux上出现错误(预期行为)


test.cpp:2:错误:存储类说明符在参数声明中无效

test.cpp:2:error:为参数't'指定的存储类


< blockquote>

我的问题是,

为什么Windows编译器允许这种类型的违规,他们有什么优势,我看不到?

这在运行时如何运行?


我几乎没有猜测可能,其中没有一个是实际行为,

1> static关键字被忽略

2>每当函数调用(在循环中调用函数会导致程序崩溃,出现内存问题)时,将创建此变量a的多个副本。

3>只为函数创建一个静态实例,并且每次调用函数时使用相同的变量(由于内存较少,调用循环中的程序不会导致崩溃)

解决方案

根据MSDN文档,它将替换为标准存储类。它是:




  • extern ,如果标识符是一个函数。

  • 自动(如果标识符是正式参数或局部变量)。


p>参考: http://msdn.microsoft.com/en-us/library /z9d31kt4.aspx



GCC 4.8.2会产生以下致命错误:




  • 错误:参数声明中存储类说明符无效

  • 错误:为参数'a'指定的存储类



clang 3.5显示以下致命编译错误:




  • 错误:函数声明中无效的存储类说明符

  • 警告:函数'test'之前没有原型
    [-Wmissing-prototypes] |



根据第1.4节[intro.compliance],C ++标准只需要一个诊断消息:




  • 如果程序不包含违反此
    国际标准中的规则,合格的实施应在其
    资源限制内接受并正确执行该程序。

  • 如果程序包含违反任何可诊断的规则或
    发生本标准中描述的结构
    有条件支持时,实现不支持
    构造,
    <

  • 如果程序包含违反没有诊断
    的规则的违规行为



对于此警告,我建议更正通过删除静态代码。


I was reading the documentation about static and other specifiers from

here and it says

The static specifier is only allowed in the declarations of variables (except in function parameter lists ), declarations of functions (except at block scope), and declarations of anonymous unions. When used in a declaration of a class member, it declares a static member. When used in a declaration of a variable, it specifies static storage duration (except if accompanied by thread_local). When used in a declaration at namespace scope, it specifies internal linkage.

Now consider a snippet like

int test(static int a )
{
     return a;
}

int main()
{

    test(5);
    return 0;
}

Which is complete voilation of the standerd themselves.
This snippet compiles with warning on windows and runs

C4042: 'a' : has bad storage class

but gives error on Linux ( expected behavior )

test.cpp:2: error: storage class specifiers invalid in parameter declarations
test.cpp:2: error: storage class specified for parameter 't'

My question is,
Why does windows compiler allow such type of violations, Do they have any advantage which I cannot see?
How does this behave at run time?

I have few guesses Maybe, none of them is the actual behaviour,
1> static keyword is ignored
2> Multiple copies of this variable a is created whenever the function is called (calling the function in a loop should crash the program with memory issue.)
3> Only one static instance is created for the function and every time the fucntion is called the same variable is used ( Calling program in loop will not lead to a crash due to less memory )

解决方案

According to the MSDN documentation, it will be replaced by standard storage class instead. Which is either:

  • extern, if identifier is a function.
  • auto, if identifier is a formal parameter or local variable.
  • No storage class, if identifier is a global variable.

Reference: http://msdn.microsoft.com/en-us/library/z9d31kt4.aspx

GCC 4.8.2 produces the following fatal error:

  • error: storage class specifiers invalid in parameter declarations
  • error: storage class specified for parameter ‘a’

clang 3.5 shows the following fatal compilation error:

  • error: invalid storage class specifier in function declaratory
  • warning: no previous prototype for function 'test' [-Wmissing-prototypes]|

The C++ standard only requires a "diagnostic message" as per section 1.4 [intro.compliance]:

  • If a program contains no violations of the rules in this International Standard, a conforming implementation shall, within its resource limits, accept and correctly execute that program.
  • If a program contains a violation of any diagnosable rule or an occurrence of a construct described in this Standard as "conditionally-supported" when the implementation does not support that construct, a conforming implementation shall issue at least one diagnostic message.
  • If a program contains a violation of a rule for which no diagnostic is required, this International Standard places no requirement on implementations with respect to that program.

For this warning I would recommend correcting the code by removing static.

这篇关于为什么在函数参数中使用静态变量声明函数在Windows中不是错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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