Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Essential types of constant variables
#1
I'm having a disagreement with my tool vendor over the essential types of constant variables.
Code:
uint32_t v1 = 1U;
const uint32_t v2 = 1U;
static const uint32_t v3 = 1U;
constexpr uint32_t v4 = 1U;

My interpretation of the spec is that all the identifiers (v1, v2, v3 and v4) have uint32_t essential type.

My tool vendor's interpretation is that v1 has a uint32_t essential type. While v2, v3 and v4 have a uint8_t essential type.  (I don't have any insight into their reasoning)

Clarification on this issue would be much appreciated.
Reply
#2
(30-07-2021, 10:27 PM)LordMordac Wrote: I'm having a disagreement with my tool vendor over the essential types of constant variables.
Code:
uint32_t v1 = 1U;
const uint32_t v2 = 1U;
static const uint32_t v3 = 1U;
constexpr uint32_t v4 = 1U;

My interpretation of the spec is that all the identifiers (v1, v2, v3 and v4) have uint32_t essential type.

My tool vendor's interpretation is that v1 has a uint32_t essential type. While v2, v3 and v4 have a uint8_t essential type.  (I don't have any insight into their reasoning)

Clarification on this issue would be much appreciated.

The core of the disagreement revolves around this:
-----------------------------------------------------------------------------------------------------------------
The underlying type of an integer constant expression is therefore defined as follows:
1. If the actual type of the expression is signed integral, the underlying type is defined as the smallest signed type integer type that is capable of representing the data.
2. If the actual type of the expression is unsigned integral, the underlying type is defined as the smallest unsigned type integer type that is capable of representing the data.
3. In all other circumstances, the underlying type of the expression is defined as being the same actual type.
-----------------------------------------------------------------------------------------------------------------

I believe that all four instances fall into category 3.  The tool vendor engineer believes v2, v3, and v4 fall into category 2.
Reply
#3
We would agree with your tool vendor, with caveats. 

The essential type of the object v1 is definitely uint32_t. 
The essential type of the constants v2 & v3 are uint8_t, as 2008 says constants are treated as though they were literals with the appropriate value.
The constexpr v4 is out of scope of 2008 (not in C++:2003)

The rationale can be illustrated with these examples (using your definitions for v1 and v2):
      uint8_t v5 = v1;     // narrowing violation would be expected here  uint32 to unint8
      uint8_t v6 = v2;     // no violation here, because although v2 is of type uint32_t, it is known
                                  // that it is a constant that can fit in the uint8_t

However we can see why you can argue that they should treated as uint32_t. This is an area that is under review by both MISRA C and C++
Posted by and on behalf of
the MISRA C++ Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)