Can C++ compilers optimize “if” statements inside “for” loops?

后端 未结 7 1863
闹比i
闹比i 2020-12-09 03:09

Consider an example like this:

if (flag)
  for (condition)
    do_something();
else
  for (condition)
    do_something_else();

If fla

7条回答
  •  盖世英雄少女心
    2020-12-09 03:44

    As many have said: it depends.

    If you want to be sure, you should try to force a compile-time decision. Templates often come in handy for this:

    for (condition)
      do_it();
    

提交回复
热议问题