Why is declaring funciton with static variables in function arguments not a bug on Windows?
I read the documentation about static and other specs from
here and he says
The static specifier is only allowed in variable declarations (except function parameter lists) , function declarations (except block scope), and anonymous union declarations. When used in a class member declaration, it declares a static member. When used in a variable declaration, the static storage duration is specified (unless accompanied by thread_local). When used in a namespace-scoped declaration, it defines an internal binding.
Now let's look at a snippet like
int test(static int a )
{
return a;
}
int main()
{
test(5);
return 0;
}
Which is the complete veil of the tournament itself.
This snippet compiles with a warning in windows and runs
C4042: 'a': has bad storage class
but gives error on Linux (expected behavior)
test.cpp: 2: error: storage class specifiers are not valid in parameter
declarations test.cpp: 2: error: storage class specified for parameter 't'
My question is,
Why does the Windows compiler allow such violations, do they have any advantages that I don't see?
How does this behave at runtime?
I have little guesswork. Maybe none of them is real behavior,
1> static keyword is ignored
2> Multiple copies of this variable a are created whenever the function is called (calling the function in a loop should cause the program to crash with a memory problem.)
3> For only one static instance of this function is created, and each time fucntion is called, the same variable is used (calling the program in a loop will not crash due to less memory)
source to share
According to the MSDN documentation, it will be replaced with the standard storage class instead. It's the same:
- extern if the identifier is a function.
- auto if the identifier is a formal parameter or a local variable.
- The storage class if the identifier is a global variable.
Link: http://msdn.microsoft.com/en-us/library/z9d31kt4.aspx
GCC 4.8.2 generates the following fatal error:
- error: storage class specifiers are not valid 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 declarative function
- warning: no previous prototype for function 'test' [-Wmissing-prototypes] |
The C ++ standard only requires a "diagnostic message" according to [intro.compliance] section 1.4:
- If the program does not contain violations of the rules in this International Standard, the conforming implementation must, within the limits of resource constraints, accept and correctly execute this program.
- If a program contains a violation of any diagnosable rule, or the occurrence of a construct described in this International Standard as "conditionally supported" when an implementation does not support that construct, the conforming implementation MUST issue at least one diagnostic message.
- If a program contains a rule violation for which no diagnostics are required, this International Standard does not require an implementation of that program.
For this warning, I would recommend fixing the code by removing static.
source to share