I always thought that it's good to have const locals be const
void f() {
const resource_ptr p = get();
// ...
}
However last week I watched students that worked on a C++ exercise and that wondered about a const pointer being returned
resource_ptr f() {
const resource_ptr p = get();
// ...
return p;
}
Here, if the compiler can't apply NRVO (imagine some scenario under which that is true, perhaps returning one of two pointers, depending on a condition), suddenly the const
becomes a pessimization because the compiler can't move from p
, because it's const.
Is it a good idea to try and avoid const
on returned locals, or is there a better way to deal with this?
const
local anyways via as-if rule? – Cleansep
will end and move anyway? – Eclampsiastd::move(..)
around it. Butp
is const, so it won't move from it. – HendecasyllableType(Type const&&)
, it will use that. Most people consider that modifying the argument in such a constructor is wrong (it isconst
), but not all, some find it more important that it is an rvalue. In any case, I personally avoid theconst
on such variables. – Tabascoconst
to locals, especially if you expect moves from them? In the worst case, you canconst_cast
, assuming you started with a non-const
object visible outside the body of your function. Can you perhaps clarify? – Nonconductorresource_ptr
a typedef for raw pointer or what – Guthrie