casting non const to const in c++
87,517
Solution 1
const_cast
can be used in order remove or add constness to an object. This can be useful when you want to call a specific overload.
Contrived example:
class foo {
int i;
public:
foo(int i) : i(i) { }
int bar() const {
return i;
}
int bar() { // not const
i++;
return const_cast<const foo*>(this)->bar();
}
};
Solution 2
STL since C++17 now provides std::as_const
for exactly this case.
See: http://en.cppreference.com/w/cpp/utility/as_const
Use:
CallFunc( as_const(variable) );
Instead of:
CallFunc( const_cast<const decltype(variable)>(variable) );
Solution 3
You don't need const_cast
to add const
ness:
class C;
C c;
C const& const_c = c;
Please read through this question and answer for details.
Solution 4
You can use a const_cast
if you want to, but it's not really needed -- non-const can be converted to const implicitly.
Solution 5
You have an implicit conversion if you pass an non const argument to a function which has a const parameter
Author by
kamikaze_pilot
Updated on July 05, 2022Comments
-
kamikaze_pilot almost 2 years
I know that you can use
const_cast
to cast aconst
to a non-const
.But what should you use if you want to cast non-
const
toconst
? -
Jerry Coffin about 13 yearsTo whomever did the downvote: since you're apparently not going to comment, or try to point out why you think this is wrong or unhelpful, could you at least do one more unwarranted downvote, so my rep score will be a multiple of 10 again?
-
Robin Rodricks about 11 yearsYour method doesn't work in all cases ...
const_cast
needs a specific constructor for user class types. -
Motti about 11 years@Geotarget you are incorrect, no new object is created hence it doesn't matter which constructors are available.
-
bobobobo over 10 yearsYou don't need
const_cast<const Type*>
to add theconst
qualifier -- doing so is 10+ extra characters of visual noise and so this practice is IMO a Bad Idea™. Just useconst foo* = obj;
.const_cast
is most often (ab)used to takeconst
away. -
kccqzy over 10 yearsImplicit conversions from non-
const
toconst
is unreliable. Sometimes it needs to be explicit. For example, a method would distinguish between aconst
and non-const
version of a parameter in order to return avector
ofreference_wrapper
of eitherconst
ness. Consider for instance this declaration:template<typename Image> vector<reference_wrapper<typename conditional<is_const<Image>::value, const typename Image::celltype, typename Image::celltype>::type>> get_subimage(int, int, Image&);
. -
mozzbozz over 9 years
const_cast
notcosnt_cast
. Cannot edit as I would have to change at least 10 char :/ -
Aberrant over 8 yearsAs a note regarding the people saying
const_cast
is bad for this: usingconst_cast
in this way is helpful in a constructor's member initialization, where you can't make a local variable to add const. Additionally, I'd argue that the "visual noise" helps to make it obvious that something unusual is happening regarding const-ness. However, it's probably best to add a comment to clarify that you're adding const, to make sure people do not instinctively lash out at the evilconst_cast
. -
Florian Tischler about 8 yearsjust to add: as pointed out adding constness is done implicitly and therefor one can also use a
static_cast<const VarType>(var)
. I personally almost never use const_cast as it mostly makes up for software architectural flaws in your code (when used to cast away constness). So when adding constness i use either the local variable approach or a static_cast to signal there is nothing fishy going on