Static and non-static declaration of a C ++ variable in the if condition

The following conditions compile in Visual Studio C ++:

if(int x = 5) { std::cout << x; }                  1

and

if(static int x = 5) { std::cout << x; }           2

On the other hand, the gnu compiler only compiles the first one. From testing, it seems that the scope of the variable is only in the if condition.

However, since Visual Studio compiles both versions, I was wondering if there are any differences?

+4
source share
2 answers

According to the C ++ standard, GNU is correct, and VisualStudio does it wrong. Following 6.4 / 1:

condition:
    expression
    type-specifier-seq declarator = assignment-expression

type-specifier-seq, , static. , --seq, this.

+3

++ 11:

condition: 
  expression 
  attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause 
  attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list

A defect , , ++ 14, - GCC, , bugfix, , , .

+2

Source: https://habr.com/ru/post/1623531/


All Articles