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
1 answer

interprocess object passing

I need to have a class with one activity that is performed once per 5 seconds in its own thread. It is a web service one, so it needs an endpoint to be specified. During the object runtime the main thread can change the endpoint. This is my…
Adam Trhon
  • 2,915
  • 1
  • 20
  • 51
0
votes
3 answers

implicit const cast in templates

i ran across something similar to the below code snippet, which throws a compiler error because its using a const_iterator. is there a reason why vec.end() in std::copy does not implicitly get a const cast ? int main(int argc, char* argv[]) { …
keety
  • 17,231
  • 4
  • 51
  • 56
0
votes
1 answer

Advice about const (member and function)

I am studying tree exploration, I implemented a class "CNode" for the nodes where each node has a pointer member to point to his parent, my question is about a const function and a const member, see below in my code. So below is a simplified version…
0
votes
1 answer

How to use the non-const getter if there's a const one too?

If I have a class with getters of return type both const and non-const, how can I use the non-const one? The class (MeshManipulator.h): namespace vortex::meshSculptEngine { class MeshManipulator { public: ... …
Zoltán Orosz
  • 303
  • 1
  • 8
0
votes
0 answers

casting iterator returned by thrust::find_if to a struct pointer

I am using c++ for my project and facing this error in one of my functions. "a const_cast can only adjust type qualifiers; it cannot change the underlying type" I am working with thrust library and all my vectors are device type. The problem is that…
Lida
  • 1
  • 1
0
votes
0 answers

Modifying const members of non-const object in C++20

Is it possible to modify a const field of a non-const object (using const_cast) in C++20? Yes, I know it probably shouldn't be allowed, and that there are very similar questions (Move construction and assignment of class with constant member,…
Joel Niemelä
  • 168
  • 1
  • 3
  • 13
0
votes
0 answers

How can cpp show 2 different values at the same memory location after using const_cast on a const int variable

I am trying to change the value of a const int variable (should not be done ideally). This is how I did it. const int x{5}; //line 1 int* y{const_cast(&x)}; //line 2 std::cout<
0
votes
1 answer

Removing Const: Casting from std::shared_ptr to T

Is there a way to cast from T = std::shared_ptr to TCV = A please? I used this: template struct is_shared_ptr : std::false_type {}; template struct is_shared_ptr> : std::true_type…
Vero
  • 313
  • 2
  • 9
0
votes
3 answers

How do I assign to a const variable using an out parameter in C++?

In a class header file Texture.h I declare a static const int. static const int MAX_TEXTURE_SLOTS; In Texture.cpp I define the variable as 0. const int Texture::MAX_TEXTURE_SLOTS = 0; Now in Window.cpp class's constructor I attempt to assign to…
0
votes
0 answers

can C++20 concepts be used to avoid const/not const duplication?

Can C++20 concepts be used instead of templates to avoid (const/not const) code duplication ? For example (Range has nothing to do with C++20 ranges in this example), twice the same code, one const, the other not: I know there is the possibility to…
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28
0
votes
0 answers

Eigen::Ref and const_cast

In C++ I can use const_cast to convert a const Eigen::VectorXd& object to an Eigen::VectorXd& one. I wonder if it's possible to do the same with Eigen::Ref objects, i.e., is there a way to convert a const Eigen::Ref& object to…
fdev
  • 127
  • 12
0
votes
1 answer

Removing const qualifier from type-inference return type

Quick question for the folks out there. I have the function signature below that under some but not all circumstances returns a const type qualifier on the return type. template
hdamlaj
  • 51
  • 4
0
votes
0 answers

strange output after const_cast

here is my code: #include using namespace std; int main() { const int a = 10; cout << a << endl; int *b = const_cast(&a); cout << *b << endl; (*b)++; cout << *b << endl; cout << a << endl; return…
Melvin Levett
  • 341
  • 2
  • 3
  • 11
0
votes
0 answers

Could const_cast ever return a null pointer for inputs other than null pointer?

cppreference explains that one possible const_cast conversion is a "null pointer value may be converted to the null pointer value of new_type". Is there any other way const_cast can return a null pointer?
jesses
  • 559
  • 3
  • 15
0
votes
1 answer

change the value of const_cast ptr/ref doesn't change the original object value?

I found a weird problem of same address with different values after change const_cast ptr/ref object value. #include using namespace std; int main(void){ const auto i=123; auto &ref2i=i; auto ptr2i=&i; auto…
Larry
  • 155
  • 7