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

Remove const-ness from a variable

i'm trying to remove const-ness from a variable (char*), but for some reason when i try to change the value, the original value of the const variable still remains the same. const char* str1 = "david"; char* str2 = const_cast (str1); str2…
David Faizulaev
  • 4,651
  • 21
  • 74
  • 124
7
votes
2 answers

Can one override my const C++ member function returning a const pointer to a internal non-const array using const_cast?

I'm learning c++ and came across this const_cast operator. Consider the following example: class Test { private: char name[100]; public: Test(const char* n) { std::strncpy(name, n, 99); name[99]=0; } const char* getName() const {…
user803563
  • 408
  • 3
  • 11
7
votes
3 answers

Does this const initialization through const_cast have undefined behaviour?

According to my small tests this code works. But, does it have undefined behaviour? Modifying the const object through the use of const_cast resulted in run-time access violations in my previous tests, but I can't remember how they were different.…
7
votes
4 answers

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).…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
7
votes
3 answers

behavior of const_cast in C++

Here is my problem, the problem is in comments const int a = 5; const_cast(a)=7; //throw over const attribute in a,and assign to 7 std::cout<
Ryan_Liu
  • 269
  • 4
  • 9
7
votes
6 answers

What value does const void * offer over void *?

In C++, is there any value in using a const void * for an argument type to a function over a void *? Since a void * is opaque, is there any risk of modification other than if the user does reinterpret_cast, in which case they could likewise do…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
7
votes
9 answers

C++ const cast, unsure if this is secure

It maybe seems to be a silly question but i really need to clarify this: Will this bring any danger to my program? Is the const_cast even needed? If i change the input pointers values in place will it work safely with std::string or will it create…
Oliver
  • 928
  • 9
  • 25
6
votes
5 answers

Why is writing to a non-const object after casting away const of pointer to that object not UB?

According to the C++ Standard it's okay to cast away const from the pointer and write to the object if the object is not originally const itself. So that this: const Type* object = new Type(); const_cast( object )->Modify(); is okay, but…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
6
votes
2 answers

Lifetime extension of temporary by non-const reference using const-cast

This is something that came up recently and which I feel shouldn't work as it apparently does: #include #include int main() { std::shared_ptr& ptr = const_cast&>( static_cast
Steelbadger
  • 135
  • 6
6
votes
2 answers

Is it UB to call a non-const method on const instance when the method does not modify members?

Code speaks more than thousand words, so... This is undefined behaviour for mutating a const int: struct foo { int x; void modify() { x = 3; } }; void test_foo(const foo& f) { const_cast(f).modify(); } int main(){ const foo…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
6
votes
1 answer

Compiler switch to disable const_cast semantics in c-style casts?

Recently I stumbled over code such as this: void foo(const Bar* b) { ... takes_nonconst_param_fn((Bar*)b); ... Obviously, the developer didn't know what he was doing, but if the compiler hadn't silently accepted the c-style-cast and at least…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
6
votes
0 answers

Can we const-cast a non-class prvalue to xvalue?

Consider this example from 7.6.1.10, paragraph 3 [expr.const.cast] (N4810): typedef int *A[3]; // array of 3 pointer to int typedef const int *const CA[3]; // array of 3 const pointer to const int ... A &&r2 = const_cast(CA{}); // OK So, the…
6
votes
1 answer

Const casting empty base class

Is it undefined behavior to const_cast away an empty base class and call a non const method on it? For example class EmptyBase { public: void bar() { ... } }; class Something : public EmptyBase { public: void foo() const { …
Curious
  • 20,870
  • 8
  • 61
  • 146
6
votes
3 answers

Filling a std::array at compile time and possible undefined behaviour with const_cast

It is known that std::array::operator[] since C++14 is constexpr, see declaration below: constexpr const_reference operator[]( size_type pos ) const; However, it is also const qualified. This causes implications if you want to use the subscript…
101010
  • 41,839
  • 11
  • 94
  • 168
6
votes
2 answers

Is an attempt to modify a const_cast-ed, but dynamically allocated constant object still undefined behavior?

For example: const int* pc = new const int(3); // note the const int* p = const_cast(pc); *p = 4; // undefined behavior? In particular, can the compiler ever optimize away the heap-allocated *pc? If not, does an attempt to modify…
1 2
3
15 16