0

constexpr sometimes implies const

Should one - in those cases - always prefer constexpr over const due to for example run time speed-ups or are there any down sides?

holzkohlengrill
  • 1,044
  • 19
  • 29
  • 1
    I didn't ask about what the difference between `constexpr` and `const` is but rather if you should prefer one over the other. In [this](http://stackoverflow.com/a/19137473/4773274) answer @DarioOO asked the same question without getting an answer. So, I don't understand the downvoter(s) and why it should be a duplicate to the mentioned question. – holzkohlengrill May 26 '16 at 09:25

2 Answers2

3

In short, yes, you should prefer constexpr as long as your expression can be and should be evaluated at compile time.

See this question for more detail: Difference between `constexpr` and `const`

Community
  • 1
  • 1
swang
  • 5,157
  • 5
  • 33
  • 55
1

constexpr means that the compiler knows it is constant and can compute its value. This allows you to use it in compile-time constructs such as templates, and potentially allows the compiler to do exact optimizations.

If it can be constexpr, let it be.

Elazar
  • 20,415
  • 4
  • 46
  • 67