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
0
votes
0 answers

When and why const_cast is necessary for converting non-const to const types?

When and why const_cast is necessary for converting non-const to const types? Why in the following code const-cast is necessary for line 8 and unnecessary for lines 6, 7, 9? int a = 27; // 1 int const b = 412; …
Roma
  • 55
  • 8
0
votes
1 answer

assign non-static const member in class with const_cast

Why can a non-static const member be assigned using const_cast, and when would this be useful? In the following code, both A and B classes compile and work fine. However, for B, its const member s is not initialized using an initializer list in…
andreipb
  • 79
  • 8
0
votes
4 answers

const_cast(char* const) not lvalue?

When compiling the code below, I am getting an error on line 3 about the result of const_cast not being an lvalue. Is this only a problem because I used gcc 7.x (even though it is supposed to be fully C++17 compliant)? Or is this indeed invalid code…
0
votes
0 answers

How to use const cast with pointers in C++?

I am a beginner in C++ and have truble understanding the following sniped: #include int main() { const int i { 100 }; const int* ip = &i; // okay, because ip points to const value *ip = 0; // error, because i is const …
User12547645
  • 6,955
  • 3
  • 38
  • 69
0
votes
1 answer

shared_ptr to shared_pointer

I have a function which returns a shared pointer of type const A. std::shared_ptr< const A> getPointer() const; and I have a function which needs a shared_ptr of type A. void foo(std::shared_ptr a); When I try to call my function I get the…
user7431005
  • 3,899
  • 4
  • 22
  • 49
0
votes
2 answers

why does const_cast from and to a const char * result in strcmp != 0

I'm coding Bluetooth on an ESP32 using Arduino IDE. Having a little trouble with const_cast const char *. Reading the paired device value has a '\n' line feed that I want to remove. So I cast it an editable char * then convert it back to a const…
Rick_CBR929RR
  • 197
  • 2
  • 15
0
votes
2 answers

convert from 'const QVector>' to 'QVector>'

How can I solve the following, ie, converting const QVector> to QVector>? I tried a few steps but didn't help: QVector> points = const_cast>>(abc.points); abc.points is a struct…
Sayan Bera
  • 135
  • 2
  • 16
0
votes
3 answers

how can const_cast(s), while s is a string type?

I asked a related, tedious question before and now I discover something new. #include #include using namespace std; void hello (const string &s1) { cout << "rocky" << endl; } void hello(string &s1) { cout << "banana" <<…
Rick
  • 7,007
  • 2
  • 49
  • 79
0
votes
0 answers

using const_cast on constant data type

As much as I know, const_cast may be used to cast away (remove) constness or volatility. But I could not understand the execution of the code below. #include int main() { const int i = 20; int *p = const_cast(&i); …
Sourav
  • 145
  • 1
  • 14
0
votes
3 answers

casting const to pass it to function that takes reference, what happens?

Can anyone tell me what happens here when passing to g in the main, is it static_cast? int & g (int&x){x++ ; return x ; } int main() { const int a=5 ; cout<
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
0
votes
2 answers

const_cast seems to be ignored with C++ templates?

I wrote a simple logging class that supports variadic templates in C++ using Visual Studio on Windows. I created a generic Log function template with a number of specializations to cater for a common combination of possible inputs. #pragma…
MathuSum Mut
  • 2,765
  • 3
  • 27
  • 59
0
votes
0 answers

auto with const_cast reference behaves strange

Can anyone help me to understand this "weird" behavior? I was playing around with c++11 after long pause in c++ programming. Why everything works fine until I use auto? static void printIt(int a,const int *b, const int &c) { std::cout <<…
Jacinto Resende
  • 343
  • 2
  • 13
0
votes
4 answers

Referencing string with pointers

Possible Duplicate: Why does simple C code receive segmentation fault? Why code snippet 2 doesn't behave like snippet 1? //Code snippet 1 char pstr[] = "helloworld"; char *p = pstr; p[2] = 'd'; //Code snippet 2 char *p = "helloworld"; p[2] =…
0
votes
0 answers

Possible implementation of std::addressof

This could be a duplicate[link] but, the answer is rather short and not very clear. In a possible implementation like this: template T * addressof(T & v) { return reinterpret_cast(& const_cast(reinterpret_cast
user2338150
  • 479
  • 5
  • 14
0
votes
2 answers

c++ when the const is not an explicit one?

I searched the Internet and the StackOverflow about the const_cast<> and the confusion it causes, I found useful things, however I still has a question. Considering this code, #include using namespace std; int main(void) { const int…
Shadi
  • 1,701
  • 2
  • 14
  • 27