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
6
votes
5 answers

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…
legends2k
  • 31,634
  • 25
  • 118
  • 222
6
votes
2 answers

is it ok to use const_cast in member routines to avoid duplicated code

Is it okay to use const_cast in the following case, or are there any caveats: class A { public: A() : m_someData(5) {} int& get() { return m_someData; } const int& get() const { return *const_cast(this)->get(); } private: int…
vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
6
votes
5 answers

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;…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
6
votes
3 answers

Does casting away constness from "this" and then changing a member value invoke undefined behaviour?

In a response to my comment to some answer in another question somebody suggests that something like void C::f() const { const_cast( this )->m_x = 1; } invokes undefined behaviour since a const object is modified. Is this true? If it isn't,…
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
5
votes
1 answer

Variable Value Changes By Itself

I've been pretty confused while programming before, but this one takes the cake. Basically I set the value in one for loop, and in the following iteration it changes to the value of the next one. for (int i = 0; i < 2; ++i) { for (int j = 0; j <…
chris
  • 60,560
  • 13
  • 143
  • 205
5
votes
1 answer

Is const_cast valid in a constant expression? (C++14, C++17)

The specific issue that a came across is that there is some inconsistency in how compilers handle it. For instance this code (https://godbolt.org/z/08Z-zi): constexpr auto value = 1; static_assert(*const_cast(&value), "value should be…
5
votes
1 answer

C++17 Standard - Cast away const of static

Recently I decided to dive into the C++ Standard and check whether certain code snippets are well defined and where to find those definitions in the standard. Since the standard is rather hard to get right (especially if you are not used to it) I…
5
votes
1 answer

static_cast taking away constness

From my knowledge (and this topic: When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?) const_cast is the only cast that should be able to take away constness of a variable. However, when messing around with clang-6.0 and…
Leśny Rumcajs
  • 2,259
  • 2
  • 17
  • 33
5
votes
2 answers

Is there a reason to use const_cast on a string literal in this code?

I'm looking at some sample code for an API I'm about to start using. The following pattern has me a bit confused: char* str; str = const_cast("Hello World"); printf("%s ", str); (actually there's a huge case statement in which str is…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
5
votes
4 answers

is using const_cast regularly as a design tool acceptable?

I reviewed the kind of code below, and while I have a personal answer to the question (*), I'd like to have comments from C++/design experts. For some reason, Data is an object with a non-modifiable identifier, and a modifiable value: class Data { …
paercebal
  • 81,378
  • 38
  • 130
  • 159
5
votes
2 answers

Casting Const Pointer To Work With Legacy C Code

As a follow up to my previous question (Variable Length Array Performance Implications (C/C++)), I am having a bit of trouble maintaining const correctness using the C system call writev(). Namely, it appears as though I have run into the exact…
It'sPete
  • 5,083
  • 8
  • 39
  • 72
5
votes
4 answers

static_cast and reference to pointers

Can anyone tell me why this doesn't compile: struct A { }; struct B : public A { }; int main() { B b; A* a = &b; B* &b1 = static_cast(a); return 0; } Now, if you replace the static cast with: B* b1 = static_cast(a); then it does…
PierreBdR
  • 42,120
  • 10
  • 46
  • 62
5
votes
3 answers

What's the difference between char * and const_cast(string.c_str())

I use an external library to deal with udp (OSC) communication between 2 apps. To format the messages that will be sent, the library is expecting a char* but I get a string from the UI that I have to convert. While I was dealing with other parts…
err0r_500
  • 103
  • 2
  • 6
4
votes
3 answers

const_cast doesn't work c++?

I have the following code : const int k=1; int *p=const_cast( &k); cout<<"k before="<<*p<( &k)=12; cout<<"k after="<
Khaledvic
  • 534
  • 4
  • 16
4
votes
1 answer

How does std::map::extract() allow changing the key?

I am writing a template class similar to std::map. Currently I'm working on implementing a function equivalent to std::map::extract(). This should return a node handle with its own function node_type::key(), that returns a non-const reference to the…
chiasmos
  • 110
  • 7