Why GCC does not evaluate constexpr at compile time?

前端 未结 1 743
一向
一向 2021-01-12 22:14

As an example:

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

th

相关标签:
1条回答
  • 2021-01-12 22:29

    Why would g++ do that?

    constexpr functions only must be evaluated at compile time in situations where the result is used as a constant expression. These include things like initializing a constexpr variable and being used as a template argument.

    In other situations, even when a constexpr function is invoked with arguments that are all themselves constant expressions, it is up to the implementation to do what it wants. Typically, it'll depend on the optimization flags. On both gcc 6.2 and clang 3.9.1, for instance, -O0 will emit a call at runtime but -O1 will emit the constant 36000.

    0 讨论(0)
提交回复
热议问题