Local declaration parses as a function, not an object
ID |
c.correctness.most_vexing_parse |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
C / C++ |
Description
Reports a local statement of the form Type name(); inside a function body. Despite the intent usually being to default-construct a local object, this syntax declares a function named name taking no arguments and returning Type - it declares nothing of the object kind at all.
Rationale
C++ grammar resolves anything that can be parsed as a declaration as a declaration, so Timer t(); is a function declaration in the surrounding scope, not a Timer object built with its default constructor. Any later use of t as a variable - member access, passing it by value, or using it in an expression - fails to compile with an error that talks about function types, which is confusing when nothing in the line looked like a function.
The following code illustrates the pattern detected by this rule:
void useTimer();
void run()
{
// FLAGGED: Local declaration parses as a function, not an object
Timer t();
// FLAGGED: Local declaration parses as a function, not an object
int x();
Timer t2;
Timer t3{};
int y = 5;
useTimer();
}