Thread overview
compiler allows assign to const variable
Mar 31, 2004
Steve Strand
Apr 01, 2004
-scooter-
Apr 02, 2004
Heinz Saathoff
Apr 03, 2004
Matthew
Apr 06, 2004
Scott Michel
March 31, 2004
Version 8.40.2 of the compiler does not catch this const violation.


struct test {
    int data[5];
};

void foo(test const& aa)
{
    aa.data[0]= 0;      //compiler allows assign to const variable
}

int main()
{
    test aa;
    foo(aa);
}


April 01, 2004
Steve Strand wrote:

> Version 8.40.2 of the compiler does not catch this const violation.
> 
> 
> struct test {
>     int data[5];
> };
> 
> void foo(test const& aa)
           ^^^^^^^^^^ - constant reference, not constant data?

I read that declaration as "a constant reference to test", not "a reference to constant test", which would be "const test &".

constant references are redundant, since a reference can't be reseated. Thus, it's not a bug, it's really a feature. :-)


-scooter
April 02, 2004
Hello,

-scooter- wrote...
> > void foo(test const& aa)
>             ^^^^^^^^^^ - constant reference, not constant data?
> 
> I read that declaration as "a constant reference to test", not "a reference to constant test", which would be "const test &".

No, aa is a reference to a const test. The above declaration is the same
as
    void foo(const test& aa);

I don't know if const is allowed for references (I assume not because it
makes no sense as you already stated), but if, than a const ref could be
declared as
     void foo(test &const a);

Using a pointer instead of reference such a declaration makes sense:
     void foo(test * const a);  // a is a const pointer to test


- Heinz
April 03, 2004
"-scooter-" <scottm@cs.ucla.edu> wrote in message news:c4ho5t$pnd$1@digitaldaemon.com...
> Steve Strand wrote:
>
> > Version 8.40.2 of the compiler does not catch this const violation.
> >
> >
> > struct test {
> >     int data[5];
> > };
> >
> > void foo(test const& aa)
>             ^^^^^^^^^^ - constant reference, not constant data?
>
> I read that declaration as "a constant reference to test", not "a
reference
> to constant test", which would be "const test &".

Then you read it wrong. It is a reference to const data, whether

    void foo(test const& aa);

or

    void foo(const test& aa);

What you're talking about would be a

    void foo(test& const aa);

which is not allowed because it is superfluous. A reference cannot be "re-pointed", so there's no need to account for such a qualifier. That's the whole point of references.




April 06, 2004
Matthew wrote:

>> > Version 8.40.2 of the compiler does not catch this const violation.
>> >
>> >
>> > struct test {
>> >     int data[5];
>> > };
>> >
>> > void foo(test const& aa)
>>             ^^^^^^^^^^ - constant reference, not constant data?
>>
>> I read that declaration as "a constant reference to test", not "a
> reference
>> to constant test", which would be "const test &".
> 
> Then you read it wrong. It is a reference to const data, whether
> 
>     void foo(test const& aa);
> 
> or
> 
>     void foo(const test& aa);

I sit corrected. After looking at Dan Saks' writing on the subject (his wife teaches CS at my undergrad institution): While legit, I don't agree with the notation as it leads to incorrect interpretation, like mine. Of course, I've disagreed with Dan before.