How to make a for loop variable const with the exception of the increment statement?

后端 未结 9 1194
天涯浪人
天涯浪人 2020-12-24 04:26

Consider a standard for loop:

for (int i = 0; i < 10; ++i) 
{
   // do something with i
}

I want to prevent the variable i fr

9条回答
  •  北海茫月
    2020-12-24 05:11

    Couldn't you just move some or all the content of your for loop in a function that accepts i as a const?

    Its less optimal than some solutions proposed, but if possible this is quite simple to do.

    Edit: Just an example as I tend to be unclear.

    for (int i = 0; i < 10; ++i) 
    {
       looper( i );
    }
    
    void looper ( const int v )
    {
        // do your thing here
    }
    

提交回复
热议问题