Relational operators and plain chars and wchar_t

Moderators: david ward, misra cpp

Post Reply
gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

Relational operators and plain chars and wchar_t

Post by gs » Fri Mar 26, 2010 3:41 pm

The documentation for MISRA C++ rule #4-5-3 includes the following:
The relational operators <, <=, >, >= may be used to determine if a character (or wide character) represents a digit.
The document then includes several examples which demonstrate compliance (or lack thereof) for the '>=' and '<=' operators. However, I am a little uncertain how one would construct a compliant example for the use of '<' or '>'. Is the intention to require a character literal as one of the operands? If so, the question as to which literal to use arises, since the ISO standard only requires that the digits “0” to “9” have consecutive numerical values.

If, however, the intention is for the comparison to be made against either

Code: Select all

'0' - 1
or

Code: Select all

'9' + 1
, the use of '<' or '>' will always result in a rule #4-5-3 violation.

I have this feeling I have missed something. I just do not know what that something is.

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

Re: Relational operators and plain chars and wchar_t

Post by misra cpp » Mon Oct 05, 2015 2:51 pm

The use of "<" and ">" is permitted. For example:

Code: Select all

if ( ( x < '0' ) || ( x > '9' ) )
{
   /* Not a digit */
}
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.4 Standard conversions (C++)”