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

removing constness in templated function

I have a little issue with removing the constness of using Templated function. #include using namespace std; template< typename T> void fct(T& param) { const_cast(param) = 40; } int _tmain(int argc, _TCHAR* argv[]) { int x…
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
3
votes
2 answers

Using const_cast for creating non-const variant of methods

Can the const_cast be used for creating non-const versions of already implemented methods? I think I saw something along these lines (with recommendation for the const method to do the actual work), but I'm not really sure how it's supposed to…
ValentaTomas
  • 260
  • 3
  • 11
3
votes
1 answer

Casting away const-ness of method

As a follow up to my previous question (Writing to class member through const &), is it also well defined and correct to cast away a class member method's const-ness like this? class A { public: A() : a(5) { } int run() const …
Martin G
  • 17,357
  • 9
  • 82
  • 98
3
votes
1 answer

Modification of elements of std::set - Defined behavior?

Given the following code: #include struct X { int a, b; friend bool operator<(X const& lhs, X const& rhs) { return lhs.a < rhs.a; } }; int main() { std::set xs; // some insertion... auto it = xs.find({0,…
Holt
  • 36,600
  • 7
  • 92
  • 139
3
votes
6 answers

STL std::map, pass by ref to const and the necessity of const_casting

I have a simple question regarding const_cast and best practices regarding STL containers. Consider the following where class Foo has a private STL std::map from Widget* to int: Declaration: #include using std::map; class Widget; class Foo…
Marc
  • 233
  • 2
  • 7
3
votes
1 answer

Passing a const lvalue reference as rvalue reference

I have a function that accepts an rvalue reference: template void foo(std::tuple&& t) { processFoo(std::forward>(t)); } and another function that accepts an lvalue reference: template void…
Sergey
  • 7,985
  • 4
  • 48
  • 80
3
votes
3 answers

const_cast and Shooting your own Foot

I realize that this results in undefined behavior: const int x = 12; *const_cast(&x) = 13; But does gcc do anything to prevent this from killing you, or does it just let you stand back and say "Hey you know better". Maybe it just silently…
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
3
votes
2 answers

Can we convert a const vector to vector using const_cast?

Please forgive me if it is very basic. I like to use const_cast for the conversion. If it is possible can anyone please share the example. Other way which I'm avoiding is to iterate const vector and insert its content into a new vector.
Karn
  • 79
  • 8
3
votes
2 answers

conversion between 2 types with different const qualifiers

This is a short example of the code I want to use: template class B { public : bool func1(const T& t) { // do something } }; class A { B b; public: void func2(const int* a) { b.func1(a); …
Adam
  • 464
  • 6
  • 16
3
votes
2 answers

const_casting element type of container

Is there an efficient and safe way to cast a std::vector& to std::vector&? Doing reinterpret_cast&>(constvec) would probably work correctly but is probably undefined behavior. The only standard option seems…
tmlen
  • 8,533
  • 5
  • 31
  • 84
3
votes
2 answers

When is it ok to modify a value when you remove const with const_cast?

According to §7.1.​5.1/4: Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior. So my question becomes: when is an object a const…
Martin C. Martin
  • 3,565
  • 3
  • 29
  • 36
3
votes
1 answer

const_cast conversion to lvalue reference doesn't remove constness

I would like to understand a difference of the below two cases. const uint32_t v0 = 0; const uint32_t v1 = 1; const_cast(v0) = v1; std::cout << v0 << std::endl; This results: 0 However, struct S { const uint32_t v0; S() : v0(…
wflwfl
  • 77
  • 3
3
votes
1 answer

Cast or convert a pointer (T*) to a two-const (T const * const) pointer

C++ I want to know if a pointer that isn’t already a two-const pointer (e.g. T const * const) can be implicitly or explicitly cast, processed through something (e.g. a function), or otherwise converted, to yield a T const * const, without or before…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
3
votes
2 answers

C++. How can I const_cast pointer to member function?

#include template struct mem_fun_ptr_t { typedef ReturnType (Type::*Func)(); Func func; public: mem_fun_ptr_t(Func f): func(f) {} ReturnType operator () (Type *p) { return…
yivo
  • 3,324
  • 1
  • 18
  • 23
3
votes
2 answers

In C++, reference as class member shall be const or non-const

Consider the following minimal example. #include class Data { std::vector& v; public: Data(std::vector& _v) : v(_v) {} Data(const std::vector& _v) : v(_v) {} // error! }; int main() { std::vector v; …
Johannes
  • 2,901
  • 5
  • 30
  • 50