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
constdocuments fact won't ever changed. may in instances make function more readable. - declaring variable
constaffects overload resolution, , may makeh(x)resolvehdifferently depending on whetherxconst.
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
Post a Comment