Why doesn't GCC evaluate constexpr at compile time?

As an example:

class something {
public:
  static constexpr int seconds(int hour, int min, int sec)
  { return hour*3600+min*60+sec; }
}

then

printf("Look at the time: %d\n", something::seconds(10, 0, 0));

Compiles a function call with g ++ instead of putting a constant number. Why does g ++ do this? There is no gain in it, and the goal of using constexpr instead of terrible macros seems to be striking.

+4
source share
1 answer

Why does g ++ do this?

constexprfunctions should only be evaluated at compile time in situations where the result is used as a constant expression. This includes things like initializing a variable constexprand using it as a template argument.

, constexpr , , , . , . , gcc 6.2, clang 3.9.1, -O0 , -O1 36000.

+14

Source: https://habr.com/ru/post/1667820/


All Articles