mac mohan
mac mohan

Reputation: 175

What are Legitimate uses of const_cast

Since with help of const_cast anyone can modify my declared constant object - what use is the const qualifier?

I mean how can someone ensure that what he declared const is not going to be modified anyway?

Upvotes: 15

Views: 5360

Answers (4)

AnT stands with Russia
AnT stands with Russia

Reputation: 320531

Nobody can modify your constant object, with const_cast or without it. const_cast does not allow one to modify constant objects.

When it comes to removing constness, the purpose of const_cast is to allow you remove constness from an access path (pointer or reference) that leads to a non-constant object.

For example

int i = 5;
const int *p = &i;

*const_cast<int *>(p) = 10;
assert(i == 10);

In the above code const_cast is used to gain modifying access to object i through pointer p. Object i is not constant itself, so there's nothing wrong with that modification.

That's the purpose of const_cast. It can also be used for the opposite purpose: adding constness to pointer or reference type. But "modifying constant objects" is not something you can do with const_cast. Constant objects are not modifiable.

Upvotes: 9

SingerOfTheFall
SingerOfTheFall

Reputation: 29966

const_cast is only safe if you are adding const to an originally non-const variable. Trying to remove the const status from an originally-const object, and then perform the write operation on it will result in undefined behavior.

This question is related.

Also, the msdn page says (emphasis mine):

A pointer to any object type or a pointer to a data member can be explicitly converted to a type that is identical except for the const, volatile, and __unaligned qualifiers. For pointers and references, the result will refer to the original object. For pointers to data members, the result will refer to the same member as the original (uncast) pointer to data member. Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior. You cannot use the const_cast operator to directly override a constant variable's constant status.

Upvotes: 7

Yu Hao
Yu Hao

Reputation: 122383

You are right, uses of const_cast often indicates a design flaw, or an API that is out of your control.

However, there is an exception, it's useful in the context of overloaded functions. I'm quoting an example from the book C++ Primer:

// return a reference to the shorter of two strings
const string &shorterString(const string &s1, const string &s2)
{
    return s1.size() <= s2.size() ? s1 : s2;
}

This function takes and returns references to const string. We can call the function on a pair of non-const string arguments, but we’ll get a reference to a const string as the result. We might want to have a version of shorterString that, when given non-const arguments, would yield a plain reference. We can write this version of our function using a const_cast:

string &shorterString(string &s1, string &s2)
{
    auto &r = shorterString(const_cast<const string&>(s1),
                            const_cast<const string&>(s2));
    return const_cast<string&>(r);
}

This version calls the const version of shorterString by casting its arguments to references to const. That function returns a reference to a const string, which we know is bound to one of our original, non-const arguments. Therefore, we know it is safe to cast that string back to a plain string& in the return.

Upvotes: 15

Some programmer dude
Some programmer dude

Reputation: 409186

One possible use for const_cast to cast away constness, is to call C functions that don't use const arguments but still doesn't modify their arguments, or only modifies their local copies.

Casting away the const part and then modify the data will most certainly lead to undefined behavior otherwise.

Upvotes: 4

Related Questions