6.5 - variable kinds suitable for use as loop counter

Moderators: david ward, misra cpp

grunwald
Posts: 12
Joined: Wed May 14, 2014 1:02 pm
Company: Axivion GmbH

6.5 - variable kinds suitable for use as loop counter

Postby grunwald » Thu Feb 16, 2017 6:48 pm

Which of the following is a 'variable' and thus suitable for use as loop counter (and subject to the restrictions on loop-control-variables)?

  • local variable
  • static/global variable
  • class member variable
  • function parameter

    Code: Select all

    void f(int x) { for (; x<10; ++x) {}}
  • reference

    Code: Select all

    void f(int& x) { for (x=0; x<10; ++x) {}}

The C++ standard defines "variable" to include all of the above except for non-static class members.

However, other Misra rules like 0-1-3 seem to consider "variable" to include non-static class members and even bit fields.

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

Re: 6.5 - variable kinds suitable for use as loop counter

Postby misra cpp » Mon Jul 10, 2017 11:14 am

All your examples are acceptable as loop counters
Posted by and on behalf of
the MISRA C++ Working Group


Return to “6.6 Statements (C++)”

Who is online

Users browsing this forum: No registered users and 1 guest