const-cast Questions
2
Solved
Is it okay to use const_cast in the following case, or are there any caveats:
class A {
public:
A() : m_someData(5) {}
int& get() { return m_someData; }
const int& get() const { return...
Dexamethasone asked 17/3, 2014 at 8:58
1
Solved
According to [expr.cast]/4, a C-style cast tries the following casts in order:
const_cast
static_cast
static_cast followed by const_cast
reinterpret_cast
reinterpret_cast followed by const_cast
...
Recriminate asked 30/3, 2018 at 21:47
5
Solved
So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair<Key, Value> in my iterator class (since I ...
Collect asked 29/11, 2011 at 4:18
4
Why is the following?:
const int i0 = 5;
//int i1 = const_cast<int>(i0); // compilation error
int i2 = (int)i0; // okay
int i3 = 5;
//const int i4 = const_cast<const int>(i3); // c...
Jodhpur asked 16/12, 2008 at 6:30
2
Solved
This is something that came up recently and which I feel shouldn't work as it apparently does:
#include <iostream>
#include <memory>
int main()
{
std::shared_ptr<int>& ptr ...
Phosphatase asked 4/12, 2019 at 12:18
2
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...
Kat asked 1/10, 2019 at 15:13
8
Solved
I am looking to do something like this (C#).
public final class ImmutableClass {
public readonly int i;
public readonly OtherImmutableClass o;
public readonly ReadOnlyCollection<OtherImmutab...
Potshot asked 29/8, 2019 at 12:29
1
Solved
Does the following program have undefined behavior?
#include <iostream>
#include <vector>
struct Foo
{
const std::vector<int> x;
};
int main()
{
std::vector<int> v = {1...
Fatuitous asked 3/5, 2019 at 22:50
0
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...
Lamp asked 3/5, 2019 at 1:59
8
Solved
As a common rule, it is very often considered a bad practice to use const_cast<>() in C++ code as it reveals (most of the time) a flaw in the design.
While I totally agree with this, I howev...
Viscountcy asked 20/4, 2010 at 8:2
1
Solved
Recently I decided to dive into the C++ Standard and check whether certain code snippets are well defined and where to find those definitions in the standard. Since the standard is rather hard to g...
Encounter asked 18/2, 2019 at 10:6
4
Solved
The following C++ code does not compile because it's passing a non-const pointer to a find() function which expects a const pointer.
#include <map>
std::map<int*, double> mymap;
doub...
Serajevo asked 8/2, 2019 at 8:17
1
Solved
Is the following allowed:
const int const_array[] = { 42 };
int maybe_inc(bool write, int* array) {
if (write) array[0]++;
return array[0];
}
int main() {
return maybe_inc(false, const_cast&l...
Thomas asked 3/2, 2019 at 15:11
3
Solved
I understand that const_cast works with pointers and references.
I'm assuming that the input to const_cast should be a pointer or reference. I want to know why it doesn't remove the constness if t...
Capon asked 31/8, 2018 at 8:1
1
Solved
From my knowledge (and this topic: When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?) const_cast is the only cast that should be able to take away constness of a varia...
Linders asked 25/8, 2018 at 21:24
1
struct foo
{
const int A;
int B;
foo() : A(10), B(20) {}
};
void main()
{
foo f1;
const_cast<int&>(f1.A) = 4; //line 1
const foo f2;
const_cast<int&>(f2.B) = 4; //line 2...
Boutte asked 5/6, 2018 at 14:49
2
Solved
I have a private variable in my Student class defined as:
const int studentNumnber;
I am trying to write a copy constructor for the Student and I need to cast away the constness to do this. Unfo...
Lone asked 24/10, 2013 at 0:34
1
Solved
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 ...
Phillis asked 21/12, 2017 at 6:43
3
Solved
Is there any good way to obviate the const_cast below, while keeping const correctness?
Without const_cast the code below doesn't compile. set::find gets a const reference to the set's key type, s...
Maurene asked 9/5, 2016 at 14:11
5
Suppose I have
class A final {
int& ir;
public:
A(int& x) : ir(x) { }
void set(int y) { ir = y; } // non-const method!
int get() const { return ir; }
};
and
const int i;
Obvious...
Dromous asked 21/4, 2016 at 14:57
3
Solved
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 c...
Yevetteyew asked 17/12, 2015 at 15:27
5
Solved
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();
co...
Asymptomatic asked 16/12, 2011 at 6:33
2
Solved
For example:
const int* pc = new const int(3); // note the const
int* p = const_cast<int*>(pc);
*p = 4; // undefined behavior?
In particular, can the compiler ever optimize away th...
Detta asked 10/6, 2015 at 12:20
3
Solved
I know that casting away const-ness should be done with care, and any attempt to remove const-ness from an initially const object followed by modifying the object results in undefined behaviour. Wh...
Threequarter asked 26/4, 2015 at 20:47
3
Solved
What is happening here?
const int a = 0;
const int *pa = &a;
int *p = const_cast<int*>(pa);
*p = 1; // undefined behavior ??
cout << a << *p; // ??
My compiler outpu...
Effluence asked 8/8, 2014 at 18:18
1 Next >
© 2022 - 2024 — McMap. All rights reserved.