Questions tagged [const-cast]

Anything related to the `const_cast` operation in C++, i.e. a form of compile-time conversion where the `const` or `volatile` qualifiers are cast away (removed) from a variable.

Anything related to the const_cast operation in C++, i.e. a form of compile-time conversion where the const or volatile qualifiers are cast away (removed) from a variable.

See CPPreference page on const_cast.

231 questions
11
votes
7 answers

Undefined behaviour with const_cast

I was hoping that someone could clarify exactly what is meant by undefined behaviour in C++. Given the following class definition: class Foo { public: explicit Foo(int Value): m_Int(Value) { } void SetValue(int Value) { m_Int = Value;…
Munro
  • 365
  • 1
  • 5
  • 14
11
votes
4 answers

C++ const_cast usage instead of C-style casts

Why is the following?: const int i0 = 5; //int i1 = const_cast(i0); // compilation error int i2 = (int)i0; // okay int i3 = 5; //const int i4 = const_cast(i3); // compilation error …
kenny laitin
11
votes
3 answers

Is it safe to remove const via const_cast and invoke a non-const function that does not modify the resulting object?

I know that casting away const-ness should be done with care, and any attempt to remove const-ness from an initially const object followed by modifying the object results in undefined behaviour. What if we want to remove const-ness so that we may…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
10
votes
2 answers

Efficiently const_cast-ing a constant reference parameter

I have a member function which takes a constant reference parameter to another object. I want to const_cast this parameter in order to easily use it inside the member function. For this purpose, which of the following codes is better?: void…
hkBattousai
  • 10,583
  • 18
  • 76
  • 124
10
votes
3 answers

Avoiding const_cast when calling std::set::find

Is there any good way to obviate the const_cast below, while keeping const correctness? Without const_cast the code below doesn't compile. set::find gets a const reference to the set's key type, so in our case it guarantees not to change the…
Danra
  • 9,546
  • 5
  • 59
  • 117
9
votes
5 answers

Is it possible to cast a pair to a pair?

So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair in my iterator class (since I need to modify it), but at the same time I'd like the…
masaers
  • 697
  • 9
  • 21
9
votes
2 answers

Can we use `const_cast` to modify a constant variable?

int main() { const int ia = 10; int *pia = const_cast(&ia); *pia = 5; std::cout << &ia << "\t" << pia <
micx
  • 91
  • 1
  • 2
9
votes
5 answers

const_cast in template. Is there a unconst modifier?

I have a template class like this: template class MyClass { T* data; } Sometimes, I want to use the class with a constant type T as follows: MyClass mci; but I want to modify the data using const_castdata (it is not…
danatel
  • 4,844
  • 11
  • 48
  • 62
8
votes
4 answers

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?
sharptooth
  • 167,383
  • 100
  • 513
  • 979
8
votes
7 answers

const_cast of a static const member

The following code compile well both with GCC (4.2-4.6) and with Clang (2.1), but when I run the executable it gives me "Bus error: 10". I don't understand the reason. #include struct A { static int const v; A() { ++*const_cast
mattia.penati
  • 532
  • 5
  • 18
8
votes
1 answer

Is this const_cast undefined behavior?

I was wondering whether the following is undefined behavior // Case 1: int *p = 0; int const *q = *const_cast(&p); // Case 2: (I think this is the same) int *p = 0; int const *const *pp = &p; int const *q = *pp; Is this…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
8
votes
2 answers

Where is the undefined behavior when using const_cast<>?

If I do: const char* const_str = "Some string"; char* str = const_cast(const_str); // (1) str[0] = "P"; // (2) Where (which line) exactly is the undefined behavior ? I've been searching a lot for this on SO but haven't found any explicit…
ereOn
  • 53,676
  • 39
  • 161
  • 238
8
votes
4 answers

How to find by a const pointer key in a map with non-const pointer keys

The following C++ code does not compile because it's passing a non-const pointer to a find() function which expects a const pointer. #include std::map mymap; double myfind(const int * mykey) { return…
bedrorom
  • 415
  • 5
  • 16
8
votes
1 answer

const_cast rules in c++

struct foo { const int A; int B; foo() : A(10), B(20) {} }; void main() { foo f1; const_cast(f1.A) = 4; //line 1 const foo f2; const_cast(f2.B) = 4; //line 2 } Do both line 1 and 2 exhibit undefined…
user6386155
  • 825
  • 7
  • 17
8
votes
3 answers

Is this undefined behavior with const_cast?

What is happening here? const int a = 0; const int *pa = &a; int *p = const_cast(pa); *p = 1; // undefined behavior ?? cout << a << *p; // ?? My compiler outputs 0 and 1, but address of 'a' and value of 'p' is the same, so I'm confused how…
Tracer
  • 2,544
  • 2
  • 23
  • 58
1
2
3
15 16