c++ - Can we omit const on local variables in constexpr functions? -


for example:

constexpr int g() { return 30; }      constexpr int f() {     // can omit const?     const int x = g();     const int y = 10;      return x + y; } 

is there any point ever declare local variables in constexpr function const?

aren't constexpr functions const local variables equivalent no const?

in other words, constexpr on function impose (imply) const on local variables?

the same arguments declaring variables const in non-constexpr functions apply constexpr functions:

  • declaring variable const documents fact won't ever changed. may in instances make function more readable.
  • declaring variable const affects overload resolution, , may make h(x) resolve h differently depending on whether x const.

of course, in opposite direction, mentioned in comments already:

even in constexpr functions, local variables may changed. if variables changed const, attempts change them no longer accepted.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -