Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

It is possible to devise syntax that can ambiguously be interpreted as either an expression statement or a declaration. Syntax of this sort is called a vexing parse because the compiler must use disambiguation rules to determine the semantic results. The C++ Standard, [stmt.ambig], paragraph 1 , states in part [ISO/IEC 14882-2014], states in part:

There is an ambiguity in the grammar involving expression-statements and declarations: An expression-statement with a function-style explicit type conversion as its leftmost subexpression can be indistinguishable from a declaration where the first declarator starts with a (. In those cases the statement is a declaration. [Note: To disambiguate, the whole statement might have to be examined to determine if it is an expression-statement or a declaration. ...

...

In this noncompliant code example, an attempt is made to declare a local variable, w, of type Widget while executing the default constructor. However, this declaration is syntactically ambiguous where the code could be either a declaration of a function pointer accepting no arguments and returning a Widget or a declaration of a local variable of type Widget. The syntax used in this example defines the former instead of the latter.

...

Running this program produces the expected output:

Widget constructed
Gadget constructed
Widget constructed
Gadget constructed
3

Risk Assessment

Syntactically ambiguous declarations can lead to unexpected program execution. However, it is likely that rudimentary testing would uncover violations of this rule.

...

Tool

Version

Checker

Description

 PRQA QA-C++

Include Page
PRQA QA-C++_V
PRQA QA-C++_V

2510

 
Clang
Include Page
Clang_V
Clang_V
-Wvexing-parse 

Related Vulnerabilities

Search for other vulnerabilities resulting from the violation of this rule on the CERT website.

...