Is diagnostics of const violation required

K

Kai-Uwe Bux

Please consider:

struct X {

int x;

X ( int i = 0 )
: x ( i )
{}

operator X & ( void ) const {
return ( *this );
}

};

void add_one ( X & ref ) {
++ ref.x;
}

#include <iostream>

int main ( void ) {
X const a ( 2 );
add_one( a );
std::cout << a.x << '\n';
}


This code is clearly in violation of the standard [9.3.2/2]:

In a const member function, the object for which the function is called is
accessed through a const access path; [...]


However, I wonder whether diagnostics for this violation is required -- on
my machine, the code compiles but the program segfaults and I am
considering submitting a bug report to the compiler vendor.

Can someone give me chapter and verse on this?


Thanks

Kai-Uwe Bux
 
V

Victor Bazarov

Kai-Uwe Bux said:
Please consider:

struct X {

int x;

X ( int i = 0 )
: x ( i )
{}

operator X & ( void ) const {
return ( *this );
}

This "conversion function" is not used. It cannot be used according
to 12.3.2.
};

void add_one ( X & ref ) {
++ ref.x;
}

#include <iostream>

int main ( void ) {
X const a ( 2 );
add_one( a );
std::cout << a.x << '\n';
}


This code is clearly in violation of the standard [9.3.2/2]:

In a const member function, the object for which the function is
called is accessed through a const access path; [...]

Which part do you think is in violation? I don't believe 9.3.2/2
has anything to do with it. What happens here is a violation of the
subclause 8.5.3/5 that essentially says that a reference to non-const
object cannot be initialised with an object if its 'cv-qualifiers'
are stronger than that of the reference. A diagnostic is required
according to 1.4.
However, I wonder whether diagnostics for this violation is required
Yes.

-- on my machine, the code compiles but the program segfaults and I am
considering submitting a bug report to the compiler vendor.

Can someone give me chapter and verse on this?

I have, I hope.

V
 
K

Kai-Uwe Bux

Victor said:
Kai-Uwe Bux said:
Please consider:

struct X {

int x;

X ( int i = 0 )
: x ( i )
{}

operator X & ( void ) const {
return ( *this );
}

This "conversion function" is not used. It cannot be used according
to 12.3.2.
};

void add_one ( X & ref ) {
++ ref.x;
}

#include <iostream>

int main ( void ) {
X const a ( 2 );
add_one( a );
std::cout << a.x << '\n';
}


This code is clearly in violation of the standard [9.3.2/2]:

In a const member function, the object for which the function is
called is accessed through a const access path; [...]

Which part do you think is in violation? I don't believe 9.3.2/2
has anything to do with it. What happens here is a violation of the
subclause 8.5.3/5 that essentially says that a reference to non-const
object cannot be initialised with an object if its 'cv-qualifiers'
are stronger than that of the reference. A diagnostic is required
according to 1.4.

I wasn't aware of [12.3.2]. Since my compiler didn't complain, I was under
the impression that the conversion was responsible for the initialization
of the X & from the const X object. In that case, a violation of [9.3.2/2]
would happen since the code modifies a const object.

However, I think you are right that the conversion function is immaterial
and that the code is invalid for the reasons you stated.
I have, I hope.

Yes you have. Thanks a lot.


Best

Kai-Uwe Bux
 
T

Tomás

Kai-Uwe Bux posted:
Please consider:

struct X {

int x;

X ( int i = 0 )
: x ( i )
{}

operator X & ( void ) const {
return ( *this );
}

};

void add_one ( X & ref ) {
++ ref.x;
}

#include <iostream>

int main ( void ) {
X const a ( 2 );
add_one( a );
std::cout << a.x << '\n';
}


This code is clearly in violation of the standard [9.3.2/2]:

In a const member function, the object for which the function is
called is accessed through a const access path; [...]


However, I wonder whether diagnostics for this violation is required --
on my machine, the code compiles but the program segfaults and I am
considering submitting a bug report to the compiler vendor.

Can someone give me chapter and verse on this?


Thanks

Kai-Uwe Bux


I wrote a thread recently entitled "This is ridiculous!" which deals with
the same thing. Basically, some compilers are retarded and only give a
warning if you do the following:

std::string const a;

std::string &b = a;


-Tomás
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top