const-cast

C++ const_cast usage instead of C-style casts

Why is the following?: const int i0 = 5; //int i1 = const_cast<int>(i0); // compilation error int i2 = (int)i0; // okay int i3 = 5; //const int i4 = const_cast<const int>(i3); // compilation error const int i5 = (const int)i3; // okay ...

Does const_cast ever cause actual code emission?

Is it true that const_cast is just a way to tell the compiler "stop moaning, treat this as a non-const pointer"? Are there any cases when const_cast itself is translated into actual machine code? ...

Why can't I const_cast the return of the conversion operator?

I've got a conversion operator that returns a const pointer, and I need to const_cast it. However, that doesn't work, at least under MSVC8. The following code reproduces my problem: class MyClass { public: operator const int* () { return 0; } }; int main() { MyClass obj; int* myPtr; // compiles const int...

const_cast in template. Is there a unconst modifier ?

I have a template class like this: template<T> class MyClass { T* data; } Sometimes, I want to use the class with a constant type T as follows: MyClass<const MyObject> mci; but I want to modify the data using const_cast<MyObject*>data (it is not important why but MyClass is a reference count smart pointer class which keeps the re...

Is using const_cast for read-only access to a const object allowed?

In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer: size_t countZeroes( int* array, size_t count ) { size_t result = 0; for( size_t i = 0; i < count; i++ ) { if( array[i] == 0 ) { ++result; } } return result...

Need clarifications in C-style, reinterpret, and const casts

Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is t...

Correct usage(s) of const_cast<>

Hi, As a common rule, it is very often considered a bad practice to use const_cast<>() in C++ code as it reveals (most of the time) a flaw in the design. While I totally agree with this, I however wonder what are the cases were using const_cast<>() is ok and the only solution. Could you guys please give me some examples you know/you e...

Implications of a const_cast in a copy constructor?

So I've got an output stream class that owns a pointer to a class that actually does the writing, and I need a copy constructor so that I can return initialized instances from a function so that I can bind certain values transparently to the user. To have reasonable copy semantics I'd really like to clear the writer pointer in the copie...

Is const_cast acceptable when defining an array?

I have a static const array class member (const pointers to SDL_Surfaces, but that's irrelevant), and have to loop through it in order to populate it. Aside from a const_cast when I'm done looping, which I hear is bad practice, how would I go about doing this? EDIT: The reason I don't just do... static SDL_Surface *const myArray[3]; ....

const_cast and UB

$5.2.11/7 - "[Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_cast that casts away a const-qualifier68) may produce undefined behavior (7.1.5.1). ]" The wordings of this section (C++03) are surprising to me. What is suprising are tw...