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 makeh(x)
resolveh
differently depending on whetherx
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
Post a Comment