AutosarC++17_03-A7.1.7

Each identifier shall be declared on a separate line

Required inputs: IR

Rationale

Declaring an identifier on a separate line makes the identifier declaration easier to find and the source code more readable. Also, combining objects, references and pointers declarations on the same line may become confusing.

Exceptions

It is permitted to declare an identifier in initialization statement of a for loop.

Example
// $Id: A7-1-7.cpp 271715 2017-03-23 10:13:51Z piotr.tanski $
#include <cstdint>
#include <vector>

typedef std::int32_t *ptr;            // Compliant
typedef std::int32_t *pointer, value; // Non-compliant

void fn1() noexcept {
std::int32_t x = 0;                // Compliant
std::int32_t y = 7, *p1 = nullptr; // Non-compliant
std::int32_t const *p2, z = 1;     // Non-compliant
}

void fn2() {
std::vector<std::int32_t> v{1, 2, 3, 4, 5};
for (auto iter{v.begin()}, end{v.end()}; iter != end;
++iter) // Compliant by exception
{
// ...
}
}
See Also
  • HIC++ v4.0 [8]: 7.1.1 Declare each identifier on a separate line in a separate declaration.
  • JSF December 2005 [7]: AV Rule 42 Each expression-statement will be on a separate line.
  • C++ Core Guidelines [10]: NL.20: Don’t place two statements on the same line.

Possible Messages

Key

Text

Severity

Disabled

multi_declaration

Multiple declarators in single declaration

None

False

Options

allow_in_for_loop_init

allow_in_for_loop_init : bool = True

Whether a multi-declaration is allowed in the for-init statement
 

allow_uninitialized_simple_type

allow_uninitialized_simple_type : bool = False

Whether a multi-declaration of simple type is allowed when there is no initialization