473,498 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is diagnostics of const violation required

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
Mar 15 '06 #1
7 1698
Kai-Uwe Bux wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 15 '06 #2
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
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.
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.


Yes you have. Thanks a lot.
Best

Kai-Uwe Bux

Mar 15 '06 #3
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
Mar 15 '06 #4
Tomás wrote:
[...]
I wrote a thread recently entitled "This is ridiculous!" which [...]


....which was a huge waste of bandwidth if you ask me...
Mar 15 '06 #5
Tomás wrote:
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';
}
[snip]
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;

I would be happy if I got a warning. The code I showed compiled without any
diagnostic output at the highest warning level that my compiler allows. By
and large, the standard only requires "diagnostics" and is silent about
their particular wording, e.g., whether something is labelled and error
message or a warning. Since I got nothing, it's a bug in the compiler.
Otherwise, it may or may not be a bug, depending on the documentation of
the compiler.
Best

Kai-Uwe Bux
Mar 15 '06 #6
Kai-Uwe Bux wrote:
Tomás wrote:
[snip]
I wrote a thread recently entitled "This is ridiculous!" which [...]


I would be happy if I got a warning. [...]
Since I got nothing, it's a bug in the compiler.
[...]


Please don't start again. The other thread has gone on long enough and
recently enough to justify another one on the same essential topic. If
you would like to continue the discussion [whether it's a bug or not
a bug and what diagnostic is required and what it should be marked as],
please join that thread and contribute. At least others will have
an opportunity to ignore it easier. Thanks!

V
--
Please remove capital As from my address when replying by mail
Mar 15 '06 #7
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
Tomás wrote:
[snip]
I wrote a thread recently entitled "This is ridiculous!" which [...]


I would be happy if I got a warning. [...]
Since I got nothing, it's a bug in the compiler.
[...]


Please don't start again. The other thread has gone on long enough and
recently enough to justify another one on the same essential topic. If
you would like to continue the discussion [whether it's a bug or not
a bug and what diagnostic is required and what it should be marked as],
please join that thread and contribute. At least others will have
an opportunity to ignore it easier. Thanks!


I have no intention of going on in that direction. The other thread was
about wether a warning is good enough. I have no opinion on that. I just
wanted to know whether (any) diagnosics is required for my code. It is. I
am satisfied with the information I got (from you! thanks again). As far as
I am concerned, this thread is finished.
Best

Kai-Uwe Bux
Mar 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2344
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
7
2047
by: rahul8143 | last post by:
hello, const int *ptr1 mean i can change value of ptr1 but not of *ptr1 right? then why following snippet doesnot give error? int val=50; const int *ptr1=&val; *(int *)ptr1=98; //what is this...
24
2290
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other...
41
2321
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
13
1460
by: Michal Nazarewicz | last post by:
The following code compiles fine (with warnings though) under GCC with -pedantic and -ansi arguments: #v+ int main(void) { const int a = 0; int *p = &a; return 0; } #v-
10
9280
by: lovecreatesbea... | last post by:
C stops the conversion from (char **) to (const char **). c-faq.com sec 11.10 has explanation on this point. But, for example, even the conversion from (char *) to (const char *) brings the same...
26
2081
by: karthikbalaguru | last post by:
Hi, While trying to understand the difference between the following 2 methods, i have some interesting queries. Method 1) char *s = "Hello"; and Method 2) char s = "Hello"; How does the...
15
7837
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
13
1153
by: jacob navia | last post by:
Consider this nice C program: short long n; signed unsigned b; const long const long a; unsigned double w; signed float k; short double q; unsigned long double z;
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.