J
Johannes Bauer
Hello group,
I've run into some *very* nasty trouble which I could (after hours of
work) trace to a problem in my use of the const keyword (which
appearently has not been appropriate). It is very difficult to trace, as
it only appears when the program is compiled with g++ and -O3 (-O2 and
below work fine).
So I'm guessing it's some kind of aliasing issue.
One of my classes provides an operator*=, which takes a const Fred& as a
parameter. As soon as I remove the const, it also works with -O3.
Now since it is very difficult to trace remotely, I have some general
questions about usage of const:
1. Is it always safe to do a c-style cast in which const is *added*?
I.e. I have a method which can access a foo* r/w - can it also return
(const foo*)x?
2. Is there a difference between const_cast and a c-style cast which
modifies only the const keyword?
3. In a class like this:
class foo {
private:
int *x;
public:
void blah() const {
x[9] = 123;
}
};
Is it safe to declare blah() const?
Kind regards,
Johannes
I've run into some *very* nasty trouble which I could (after hours of
work) trace to a problem in my use of the const keyword (which
appearently has not been appropriate). It is very difficult to trace, as
it only appears when the program is compiled with g++ and -O3 (-O2 and
below work fine).
So I'm guessing it's some kind of aliasing issue.
One of my classes provides an operator*=, which takes a const Fred& as a
parameter. As soon as I remove the const, it also works with -O3.
Now since it is very difficult to trace remotely, I have some general
questions about usage of const:
1. Is it always safe to do a c-style cast in which const is *added*?
I.e. I have a method which can access a foo* r/w - can it also return
(const foo*)x?
2. Is there a difference between const_cast and a c-style cast which
modifies only the const keyword?
3. In a class like this:
class foo {
private:
int *x;
public:
void blah() const {
x[9] = 123;
}
};
Is it safe to declare blah() const?
Kind regards,
Johannes