const_cast question

In the following code, does the constness of myint a get preserved?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
struct myint{
    int x;
    myint(int ix):x(ix){};
    int vect() { return x; };
};

using namespace std;
void test(const myint &z){
    int y = const_cast<myint *>(&z)->vect(); //temporary cast?
    // int v=z.vect(); error because constness is maintained?
    std::cout << "y " << y << std::endl;
}

/*
 * 
 */
int main(int argc, char** argv) {
    
    const myint a=5;
    test(a);
    return 0;
}


Thanks,
Chris
Last edited on
std::is_const <> checks whether the passed type is const:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# include <cstdlib>
# include <iostream>
# include <type_traits>
# include <iomanip>

struct myint{
    int x;
    myint(int ix):x(ix){};
    int vect() { return x; };
};

using namespace std;
void test(const myint &z){
    int y = const_cast<myint *>(&z)->vect(); //temporary cast?
    // int v=z.vect(); error because constness is maintained?
    std::cout << "y " << y << std::endl;
}

/*
 *
 */
int main(int argc, char** argv) {

    const myint a(5);
    test(a);
    std::cout << std::boolalpha << std::is_const<decltype(a)>::value << "\n";
    return 0;
}
Thanks for the quick reply. Constness is maintained.

Chris

> error because constness is maintained?
int vect() const;
@ne555

Don't quite get your point. Variable a returns true with is_const before and after the test(a) function is run. That means it stayed const, right?

The compiler was complaining about discarding qualifiers without the const_cast. I wanted to be able to run vect() without changing the constness of &z.

I am trying to integrate code into another program and did not want to mess things up by making unecessary modifications to other parts of the code.

If I understand correctly, this code:

int y = const_cast<myint *>(&z)->vect();

Is just the compiler telling me : "Q. You know z is const right? A. I know, just let me do this once"

Thanks,
Chris




I wanted to be able to run vect() without changing the constness of &z. 

this is exactly what ne555's would allow you to do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# include <cstdlib>
# include <iostream>
# include <type_traits>
# include <iomanip>

struct myint{
    int x;
    myint(int ix):x(ix){};
    int vect() const { return x; };
};

using namespace std;
void test(const myint &z){
    int y = const_cast<myint *>(&z)->vect(); //temporary cast?
     int v=z.vect(); //error because constness is maintained?
    std::cout << "y " << y << std::endl;
    std::cout << "v " << v << "\n";
}

/*
 *
 */
int main(int argc, char** argv) {

    const myint a(5);
    test(a);
    std::cout << std::boolalpha << std::is_const<decltype(a)>::value << "\n";
    return 0;
}
Last edited on
I see. I will have to try this. Making vect() a const function would be equivalent to const_cast. In actual project code, vect() returns a pointer to a writeable buffer but the class object it is a member of is const. I will have to check if it will work that way.

Thanks,
Chris
The const_cast solution does not seem to have any untoward side effects. It requires less source code modification as well in the project I am working on. I will have to pick that solution unless somebody here thinks I am creating unforeseen problems.

Thanks,
Chris
Topic archived. No new replies allowed.