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

Is it safe to do a const cast here?

I've written my own generic tree implementation, and when writing iterators for it, I am having trouble with const correctness. The issue I am having currently is as follows: This is the header file for a DFS iterator I wrote: template
c.hughes
  • 284
  • 1
  • 13
2
votes
3 answers

const cast to a global var and program crashed (C++)

int main() { const int maxint=100;//The program will crash if this line is put outside the main int &msg=const_cast(maxint); msg=200; cout<<"max:"<
OriginalWood
  • 391
  • 1
  • 4
  • 9
2
votes
1 answer

`const_cast`, top-level const and lvalue-to-rvalue conversions

Let's assume we have a simple piece of code, int main() { int x = 5; int* const y = &x; const_cast(y); } Is my assumption that a lvalue-to-rvalue conversion happens inside the const_cast expression, to read the value of y, correct? If…
2
votes
3 answers

remove const qualifier of vector in c++

Is it possible to remove constness of vector ? If so, how can it be achieved? For some reason, I don't have access to the main (), can I still remove the constness of arr in the following code snipet? It feels like that auto arr0 =…
Albert G Lieu
  • 891
  • 8
  • 16
2
votes
1 answer

C++ const_cast over a dynamic_cast and vice versa

I have a doubt about a line of the code written by my professor. This is the full code. The relevant function is: std::vector removeUnchecked() { std::vector v; CheckBox* p; for(auto it =…
2
votes
1 answer

Is it temporary materialization conversion applied to the operand of the const_cast

int main(){ const_cast(0); } According to expr.const.cast#4 For two object types T1 and T2, if a pointer to T1 can be explicitly converted to the type “pointer to T2” using a const_­cast, then the following conversions can also be…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
2
votes
1 answer

Is it safe to use std::string::c_str() to modify the underlying std::string?

I came across some code which has several instances of the bellow example: std::string str = "This is my string"; char* ptr = const_cast(str.c_str()); ptr[5] = 'w'; ptr[6] = 'a'; In this simplified example there is an assignment of…
anastaciu
  • 23,467
  • 7
  • 28
  • 53
2
votes
4 answers

For unit tests, is there a hackish way to change the value of a const variable?

I have a C++ 11 header which has a const value declared as my_const_value. And a function called GetValue that runs a complex logic using the const value and returns an expected value. I want to unit test GetValue with different values of…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
2
votes
1 answer

Const cast to non-pointer non-reference type

From [expr.const.cast]/3: For two similar types T1 and T2, a prvalue of type T1 may be explicitly converted to the type T2 using a const_­cast if, considering the cv-decompositions of both types, each Pi1 is the same as Pi2 for all i. The…
xskxzr
  • 12,442
  • 12
  • 37
  • 77
2
votes
2 answers

Understanding const correctness with conflicting requirements

Error: Invalid conversion from 'char**' to 'const char**' Similar questions did not appear to have the same set of circumstances (i.e. two functions with different const requirements on the same structure). Please only mark this as a duplicate if it…
R. Mitchell
  • 35
  • 1
  • 6
2
votes
1 answer

Accessing an inline function through a const member variable

I wanted to understand how the inline member variable work while accessing it through a const member variable. Each time I try doing so, I get an error! This is what I am trying #include #include using namespace std; class A{ …
skuila
  • 23
  • 3
2
votes
1 answer

Is modifying non-mutable member of non-const object in const method Undefined Behaviour?

dcl.type.cv provides an interesting example: For another example, struct X { mutable int i; int j; }; struct Y { X x; Y(); }; const Y y; y.x.i++; // well-formed: mutable member can be modified y.x.j++; …
Fureeish
  • 12,533
  • 4
  • 32
  • 62
2
votes
1 answer

Proper way to call a c-function taking non-const pointer arguments, const_cast, reinterpret_cast, launder

What is proper way to call a c-function taking non-const custom pointer arguments from c++? Take, as a very common example, the function fftw_plan_dft_1d from FFTW3. http://fftw.org/fftw3_doc/Complex-DFTs.html#Complex-DFTs fftw_plan…
alfC
  • 14,261
  • 4
  • 67
  • 118
2
votes
1 answer

Why does const_cast and static_cast to const reference have no effect?

In the following code: #include using namespace std; int main() { const int i = 8; int j = 90; const_cast(i) = 10; static_cast (j); j = 200; cout << " i =…
parusha
  • 53
  • 4
2
votes
2 answers

const literal versus const function as rvalue?

I have a big confusion about how a c++ compiler treating with a const variable const int constfunc() { return 7; } const int u1 = constfunc(); int* pu1 = const_cast(&u1); *pu1 = 10; cout << u1 << endl; cout << *pu1 <
Bassam Najeeb
  • 607
  • 2
  • 7
  • 16