Versions Compared

Key

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

...

However, C++ parsers are often liable to misparsing constructor arguments. While compilers will often generate a compiler error upon such misparses, it is possible for such misparses to slip past a compiler and lurk in executable code, with unexpected results.

Non-Compliant Code Example

In this non-compliant example, the class Widget has a default constructor.

...

As a result, this program compiles and prints no output, because the default constructor is never actually invoked.

Compliant Solution

This situation is ameliorated by removing the parentheses after w.

...

Running this program produces the single output constructed.

Non-Compliant Code Example

Here is a more complex non-compliant example. The class Widget maintains a single int, and the class Gadget maintains a single Widget.

...

As a result, this program compiles cleanly and prints only 3 as output, because no Gadget or Widget is constructed.

Compliant Solution

This situation is ameliorated by moving the Widget construction outside Gadget.

...

widget constructed
gadget constructed
3

Risk Assessment

Not guarding implicit constructor parsing could lead to unexpected behavior.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ31-CPP

1 (low)

3 (likely)

1 (low)

P3

L3

References

  • Wiki Markup
    \[[Meyers 01|AA. C++ References#Meyers 01]\] Item 6: Be alert for C+\+'s most vexing parse.