C++11 range-based for loops without loop variable

前端 未结 10 1168
轮回少年
轮回少年 2020-12-24 04:33

In C++ I need to iterate a certain number of times, but I don\'t need an iteration variable. For example:

for( int x=0; x<10; ++x ) {
    /* code goes her         


        
10条回答
  •  死守一世寂寞
    2020-12-24 05:18

    this works in GCC and clang and any compiler that supports the gnu attributes:

    for( [[gnu::unused]] auto x : boost::irange(0,10) ) {
    

    and should compile in any c++11 compiler but may not suppress the warning if the compiler doesn't recognise the gnu attributes.

提交回复
热议问题