Main Content

AUTOSAR C++14 Rule A7-1-3

CV-qualifiers shall be placed on the right hand side of the type that is a typedef or a using name

Description

Rule Definition

CV-qualifiers shall be placed on the right hand side of the type that is a typedef or a using name.

Rationale

Suppose a typedef or using statement defines a pointer type. For instance:

using IntPtr = std::int32_t*;
A const-qualification of the type written as:
const IntPtr ptr = &someValue;
Results in this expansion:
const (std::int32_t*) ptr = &someValue;
In this expression, ptr is a constant pointer, which cannot be reassigned to another memory location. However, a developer or reviewer might expect this expansion:
(const std::intr32_t) *ptr = &someValue;
In this expression, ptr is a pointer to a constant, which means that the contents of the location that ptr points to, or *ptr, cannot be changed.

To avoid this confusion, place a const or volatile qualifier to the right of a data type defined through typedef or using. For instance:

IntPtr const ptr = &someValue;
The only possible expansion of this expression is:
std::intr32_t const *ptr = &someValue;
which makes ptr a constant pointer.

Polyspace Implementation

The checker reports a violation if const or volatile qualifiers are placed on the left side of data types defined through typedef or using statements.

The checker reports violations on both pointer and nonpointer data types. The checker does not report a violation on typedefs defined in the std namespace.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, Polyspace reports violations of this rule when the const qualifier is placed on the right hand side of type that uses a using name. This violation is not reported for typedef-s that are part of the standard (std) namespace.

#include <cstdint>
#include <string>
using int_p = std::int32_t*;
using int_const_p = int32_t* const;
using const_int_p = const int32_t*;
void Fn(const std::uint8_t& input) // Compliant
{
	std::int32_t value1 = 10;
	std::int32_t value2 = 20;

       // deduced type is std::int32_t* const, not const std::int32_t*
	const int_p ptr1 =   // Noncompliant 
	&value1;             

	int_p const ptr2 = // Compliant
	&value1;

	int_const_p ptr3 = &value1;    // Compliant

	const_int_p ptr4 = &value1;    //Compliant
	const const_int_p ptr5 = &value1;    // Noncompliant, type is const std::int32_t* const, 
										//   not const const std::int32_t*
	const_int_p const ptr6 =
	&value1; // Compliant
	const std::string str = "Foo"; //Compliant
}

Check Information

Group: Declaration
Category: Required, Automated

Version History

Introduced in R2019a

expand all