Do C and C++ optimizers typically know which functions have no side effects?

前端 未结 4 1211
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 06:08

Say for very common math functions, such as sin, cos, etc... does the compiler realise they have no side effects and have the ability to move them to outer loops? For examp

4条回答
  •  [愿得一人]
    2020-12-13 06:44

    GCC has two attributes, pure and const, that may be used to mark such function. If the function has no side-effect and its result depends only on its arguments, the function should be declared const, if the results may also depend on some global variable the function should be declared pure. Recent versions also have a -Wsuggest-attribute warning option that can point functions which ought to be declared const or pure.

提交回复
热议问题