This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: -funswitch-loops not effective?

On Tue, Jan 18, 2005 at 10:30:08AM +0100, Richard Guenther wrote:

> At least it "works" in C++, too.  Though with C++ I may have
> extern const bool flag;
> void foo(int)
> {
>    const bool *cb = &flag;
>    bool *b = const_cast<bool*>(cb);
>    *b = !*b;
> }
> ? Or is this ill-formed?

Yes, this is undefined behaviour.

flag is a const object, whether you access it through a non-const path
or not.

-7- [Note: Depending on the type of the object, a write operation
through the pointer, lvalue or pointer to data member resulting from a
const_cast that casts away a const-qualifier* may produce undefined
behavior (

-4- Except that any class member declared mutable ( can be
modified, any attempt to modify a const object during its lifetime
( results in undefined behavior.

> Of course, this doesn't say where there is the well-formed
> leak of a pointer to the const member in the class in the first
> place:
> class Foo {
> public:
>    Foo(bool b) : flag(b) {}
>    bool get() const { return flag; }
> private:
>    const bool flag;
> };
> Is there a well-formed way to write to Foo::flag?

No, not even like this:

    struct Bar {
       mutable Foo foo;  // Foo::flag still const
    } bar;

>                                                    Couldn't we
> detect that there isn't one?

Since you've opened a PR I guess you've decided that  :-)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]