Rule 3-4-1 and defining constants in one place

Moderators: david ward, misra cpp

rrosier
Posts: 7
Joined: Tue May 01, 2018 9:34 am
Company: TomTom

Rule 3-4-1 and defining constants in one place

Postby rrosier » Thu Jul 26, 2018 1:28 pm

If one were to use the strategy of defining all the constants used within a file at the top of the file, either as static const or in an unnamed namespace, but some of the constants are used in multiple places and others are only used in single places, then those that are only used in single places will violate Rule 3-4-1.

If those that are used only in single places are subsequently moved into blocks with reduced scope, then the constants are scattered around the file.

This could create a maintenance headache because it is more difficult for a developer to find a constant.

Furthermore, if a developer needs to use a singly-used constant now in another place, they might not realise that the constant has already been defined (and then widen its scope) but instead add in a new singly-used constant of reduced scope in the new place.

It there some way to put all of the defined constants into a well-known location, but then not violate Rule 3-4-1 for the singly-used ones?

dg1980
Posts: 108
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: Rule 3-4-1 and defining constants in one place

Postby dg1980 » Thu Jul 26, 2018 6:41 pm

I don't think that this rule was meant to be relevant for constants (values that can not be changed) because the text only talks about variables (identifiers with values that can be changed).

But once again, we need an official clarification because the rule text leaves room for interpretation (which is a bad thing in most specifications btw.).

rrosier
Posts: 7
Joined: Tue May 01, 2018 9:34 am
Company: TomTom

Re: Rule 3-4-1 and defining constants in one place

Postby rrosier » Fri Jul 27, 2018 12:11 pm


misra cpp
Posts: 145
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Rule 3-4-1 and defining constants in one place

Postby misra cpp » Thu Oct 25, 2018 10:32 am

This rule was not intended to apply to constants, and will be clarified for the next version
Posted by and on behalf of
the MISRA C++ Working Group


Return to “6.3 Basic concepts (C++)”

Who is online

Users browsing this forum: No registered users and 1 guest