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 constness:

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

Share:
87,517
kamikaze_pilot
Author by

kamikaze_pilot

Updated on July 05, 2022

Comments

  • kamikaze_pilot
    kamikaze_pilot almost 2 years

    I know that you can use const_cast to cast a const to a non-const.

    But what should you use if you want to cast non-const to const?

  • Jerry Coffin
    Jerry Coffin about 13 years
    To 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
    Robin Rodricks about 11 years
    Your method doesn't work in all cases ... const_cast needs a specific constructor for user class types.
  • Motti
    Motti about 11 years
    @Geotarget you are incorrect, no new object is created hence it doesn't matter which constructors are available.
  • bobobobo
    bobobobo over 10 years
    You don't need const_cast<const Type*> to add the const qualifier -- doing so is 10+ extra characters of visual noise and so this practice is IMO a Bad Idea™. Just use const foo* = obj;. const_cast is most often (ab)used to take const away.
  • kccqzy
    kccqzy over 10 years
    Implicit conversions from non-const to const is unreliable. Sometimes it needs to be explicit. For example, a method would distinguish between a const and non-const version of a parameter in order to return a vector of reference_wrapper of either constness. 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
    mozzbozz over 9 years
    const_cast not cosnt_cast. Cannot edit as I would have to change at least 10 char :/
  • Aberrant
    Aberrant over 8 years
    As a note regarding the people saying const_cast is bad for this: using const_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 evil const_cast.
  • Florian Tischler
    Florian Tischler about 8 years
    just 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