Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++Goto page 1, 2, 3  Next

Declaration changes meaning

 
Jump to:  
 
Guest
PostPosted: Wed Aug 06, 2008 2:22 pm    Post subject: Declaration changes meaning
       
Hi,

Firstly sorry for that title of the post, but could not come up with
anything better.

I have the following code which compiles in VS2008, but gives error in
g++ indicating that the declaration of i changes meaning of i from
const int.

const int i = 255;

struct A{
int p[i];

enum { i = 128 };
};

int main(){
A f;
return 0;
}

a) Is this something to do with $3.3.6/2- "3) If reordering member
declarations in a class yields an alternate valid program under (1)
and (2), the program is ill-formed, no diagnostic is required." If
yes, then

a.1 which compiler is right?
a.2 Does an ill-formed program always require to generate compilation
error?
a.3 Is the above compilation error in GCC violating the clause "...no
diagnostic is required".

Regards,
Dabs

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Alberto Ganesh Barbati
PostPosted: Wed Aug 06, 2008 7:46 pm    Post subject: Re: Declaration changes meaning
       
rkldabs@gmail.com ha scritto:
Quote:
Hi,

Firstly sorry for that title of the post, but could not come up with
anything better.

I have the following code which compiles in VS2008, but gives error in
g++ indicating that the declaration of i changes meaning of i from
const int.

const int i = 255;

struct A{
int p[i];

enum { i = 128 };
};

int main(){
A f;
return 0;
}

a) Is this something to do with $3.3.6/2- "3) If reordering member
declarations in a class yields an alternate valid program under (1)
and (2), the program is ill-formed, no diagnostic is required." If
yes, then

Yes, it is related with that.

Quote:
a.1 which compiler is right?

Both, although the fact the code compiles in VS2008 is deplorable.

Quote:
a.2 Does an ill-formed program always require to generate compilation
error?

No. A compilation error is a (kind of) diagnostic. Each time there is a
sentence like "no diagnostic is required" the program can be ill-formed
yet... ehm... no diagnostic is required ;)

Quote:
a.3 Is the above compilation error in GCC violating the clause "...no
diagnostic is required".

No. The fact that the compiler is not required to provide a diagnostic
doesn't mean that it can't provide one.

HTH,

Ganesh

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Bo Persson
PostPosted: Wed Aug 06, 2008 7:48 pm    Post subject: Re: Declaration changes meaning
       
rkldabs@gmail.com wrote:
Quote:
Hi,

Firstly sorry for that title of the post, but could not come up with
anything better.

I have the following code which compiles in VS2008, but gives error
in g++ indicating that the declaration of i changes meaning of i
from const int.

const int i = 255;

struct A{
int p[i];

enum { i = 128 };
};

int main(){
A f;
return 0;
}

a) Is this something to do with $3.3.6/2- "3) If reordering member
declarations in a class yields an alternate valid program under (1)
and (2), the program is ill-formed, no diagnostic is required." If
yes, then

a.1 which compiler is right?

Both of them, actually.

Quote:
a.2 Does an ill-formed program always require to generate
compilation error?

No. Especially not if the rule says "no diagnostic is required".

Quote:
a.3 Is the above compilation error in GCC violating the clause
"...no diagnostic is required".

No, "not required" doesn't mean "not allowed". :-)

Some of the possible ill-formed programs are very difficult, or maybe
even impossible, to detect. If the compiler happens to detect some of
the cases, it is still a good idea to diagnose those.


Technically, an implementation just has to always output "this program
might contain an error" to comply with the letter of the standard. Or
document that the output "C++ v1.2.3" is a diagnostic. Wouldn't sell
many copies though!



Bo Persson





--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Bart van Ingen Schenau
PostPosted: Wed Aug 06, 2008 10:58 pm    Post subject: Re: Declaration changes meaning
       
rkldabs@gmail.com wrote:

Quote:
Hi,

Firstly sorry for that title of the post, but could not come up with
anything better.

I have the following code which compiles in VS2008, but gives error in
g++ indicating that the declaration of i changes meaning of i from
const int.

const int i = 255;

struct A{
int p[i];

enum { i = 128 };
};

int main(){
A f;
return 0;
}

a) Is this something to do with $3.3.6/2- "3) If reordering member
declarations in a class yields an alternate valid program under (1)
and (2), the program is ill-formed, no diagnostic is required." If
yes, then

Yes.

Quote:

a.1 which compiler is right?

Both are. This code results in undefined behaviour.

Quote:
a.2 Does an ill-formed program always require to generate compilation
error?

No. There is no construct in the standard where compilation failure is
mandated. At most, the standard requires that a diagnostic is produced,
which typically takes the form of an error or warning message.

For the large majority of cases that render a program ill-formed, a
diagnostic message is required, but sometimes it is deemed too hard to
detect a violation, so the standard explicitly absolves the compiler
from the obligation to give a diagnostic. This is indicated with the
words "no diagnostic required".

Quote:
a.3 Is the above compilation error in GCC violating the clause "...no
diagnostic is required".

No. The wording means that a compiler is not required to issue a
diagnostic, but it still may do so.
The standard never forbids a compiler from producing diagnostic
messages.

Quote:

Regards,
Dabs

Bart v Ingen Schenau

--
a.c.l.l.c-c++ FAQ: LINK
c.l.c FAQ: LINK
c.l.c++ FAQ: LINK

[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Jiang
PostPosted: Thu Aug 07, 2008 11:01 am    Post subject: Re: Declaration changes meaning
       
On Aug 7, 7:58 am, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:
Quote:
rkld...@gmail.com wrote:

[...]

Quote:

a.1 which compiler is right?

Both are. This code results in undefined behaviour.


If I read the code and standard correctly, no, there is no
undefined behavior here. The behavior is well defined,
that is, the standard say it explicitly that for the given code,
“no diagnostic is required”, which violates the diagnosable
rule necessary for a well-formed program. Since the code
is not well-formed, it is ill-formed.

Actually both "no diagnostic is required” and "undefined
behavior" violate diagnostic rule, thus result in ill-formed
code, but they are different. “no diagnostic is required”
*is* well defined behavior.

Jiang


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Jiang
PostPosted: Thu Aug 07, 2008 11:01 am    Post subject: Re: Declaration changes meaning
       
On Aug 7, 4:46 am, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
Quote:
rkld...@gmail.com ha scritto:

[...]

Quote:

a.2 Does an ill-formed program always require to generate compilation
error?

No.

To understand the rationale we need the definition of "ill-formed".

The standard says, a C + + program constructed according to the
syntax rules, diagnosable semantic rules, and the One Definition Rule
is well-formed, and if a program is not well-formed, then it is
ill-formed. (1.3.4, 1.3.14)

However, in quite a few places the standard uses explicit notation
“no diagnostic is required”, and specifies several behaviors just
undefined. For “no diagnostic is required” cases, the code is
ill-formed, but the compiler can keep silence if it can not or do
not want to detect the violations. And for undefined behavior,
the compiler can do whatever he want to do, for example, issue
a false diagnostic or send a resignation Email to your boss.

Furthermore, “no diagnostic is required” is a way to make the
implementator's life easier because issue the correct diagnostic
all the time is a challenge task. Making a [overstrict] standard is
much easier than guaranteeing all the behaviors can be
implemented under reasonable cost. [ Another reason is, well,
the percentage of compiler/library writers in standard committee
is much higher than the application writers. (joke) ]

Quote:
No. A compilation error is a (kind of) diagnostic. Each time there is a
sentence like "no diagnostic is required" the program can be ill-formed
yet... ehm... no diagnostic is required ;)


May I say

"Each time there is a sentence like "no diagnostic is required",
the program *must* be ill-formed" ?

This is clearer in my mind.

Jiang


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Alberto Ganesh Barbati
PostPosted: Thu Aug 07, 2008 3:58 pm    Post subject: Re: Declaration changes meaning
       
Jiang ha scritto:
Quote:

No. A compilation error is a (kind of) diagnostic. Each time there is a
sentence like "no diagnostic is required" the program can be ill-formed
yet... ehm... no diagnostic is required ;)


May I say

"Each time there is a sentence like "no diagnostic is required",
the program *must* be ill-formed" ?

This is clearer in my mind.


That may be clearer to you, but it would be incorrect. The phrase "no
diagnostic is required" does not imply /per se/ ill-formedness. In fact
the phrase never occurs alone, it always follows a requirement as in
"blah blah (requirement), no diagnostic is required". It's the
requirement that (if violated or not satisfied) makes the program
ill-formed. Notice that any sentence involving the verb "to shall" is a
formal requirement which imply ill-formedness if violated.

By the way, it's possibly a mistake, but there is at least one place in
the standard where "no diagnostic is required" is not attached to a
requirement. It's in 5.3.1/4: "The address of an object of incomplete
type can be taken, but if the complete type of that object is a class
type that declares operator&() as a member function, then the behavior
is undeï¬ned (and no diagnostic is required)."

HTH,

Ganesh


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Jiang
PostPosted: Thu Aug 07, 2008 7:28 pm    Post subject: Re: Declaration changes meaning
       
On Aug 8, 12:58 am, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
Quote:
Jiang ha scritto:



No. A compilation error is a (kind of) diagnostic. Each time there is a
sentence like "no diagnostic is required" the program can be ill-formed
yet... ehm... no diagnostic is required ;)

May I say

"Each time there is a sentence like "no diagnostic is required",
the program *must* be ill-formed" ?

This is clearer in my mind.

That may be clearer to you, but it would be incorrect. The phrase "no
diagnostic is required" does not imply /per se/ ill-formedness.

No. If the code has behaviors that standard specifies
"no diagnostic is required", then code is definitely
ill-formed.

Quote:
In fact
the phrase never occurs alone, it always follows a requirement as in
"blah blah (requirement), no diagnostic is required".

Please read it as

"blah blah (requirement),
[program violates such requirement is ill-formed, but]
no diagnostic is required".

So for any code which has behaviors do not need diagnostic,
it must be ill-formed.

For your original statement,

"Each time there is a sentence like "no diagnostic
is required" the program can be ill-formed yet...
ehm... no diagnostic is required Wink "

I read it as,

"Each time there is a sentence like "no diagnostic
is required" the program can be also well-formed"

Is this really your intention?


Quote:
It's the
requirement that (if violated or not satisfied) makes the program
ill-formed. Notice that any sentence involving the verb "to shall" is a
formal requirement which imply ill-formedness if violated.

True.

Quote:
By the way, it's possibly a mistake, but there is at least one place in
the standard where "no diagnostic is required" is not attached to a
requirement. It's in 5.3.1/4: "The address of an object of incomplete
type can be taken, but if the complete type of that object is a class
type that declares operator&() as a member function, then the behavior
is undeï¬ned (and no diagnostic is required)."


"no diagnostic is required" is always attached to a *violation*
of requirement [that is, attached to ill-formed code].
Since undefined behavior and "no diagnostic is required" can be
specified together, I don't see any mistake here.

To repeat myself, the point is,

1. A C++ program constructed according to the syntax rules,
diagnosable semantic rules, and the One Definition Rule,
is well-formed.

2. "no diagnostic is required" implies undiagnosable, therefore
the program *must* be ill-formed if "no diagnostic is
required" applies.

Regards,

Jiang


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Bart van Ingen Schenau
PostPosted: Thu Aug 07, 2008 9:31 pm    Post subject: Re: Declaration changes meaning
       
Jiang wrote:

Quote:
On Aug 7, 7:58 am, Bart van Ingen Schenau <b...@ingen.ddns.info
wrote:
rkld...@gmail.com wrote:

[...]


a.1 which compiler is right?

Both are. This code results in undefined behaviour.


If I read the code and standard correctly, no, there is no
undefined behavior here. The behavior is well defined,
that is, the standard say it explicitly that for the given code,
?no diagnostic is required?, which violates the diagnosable
rule necessary for a well-formed program. Since the code
is not well-formed, it is ill-formed.

Actually both "no diagnostic is required? and "undefined
behavior" violate diagnostic rule, thus result in ill-formed
code, but they are different. ?no diagnostic is required?
*is* well defined behavior.

You read the standard incorrectly.
The code in question (which was snipped) violates the requirement of
clause 3.3.6/2.
Clause 3.3.6/2 states that violation of the requirement does not have to
be diagnosed.
In clause 1.4/2, the standard states that if a program violates a
requirement for which no diagnosis is required, then the standard
places no restrictions on the compiler.
The definition of 'undefined behaviour' (in clause 1.3.12) is behaviour
for which the standard imposes no restrictions.

When combining these clauses, you reach the conclusion that violating
the requirement of clause 3.3.6/2 results in undefined behaviour.

And if the behaviour would be well-defined, then it must be possible to
tell what behaviour the compiler must exhibit when encountering this
construct (possibly after giving a diagnostic).

Quote:

Jiang

Bart v Ingen Schenau

--
a.c.l.l.c-c++ FAQ: LINK
c.l.c FAQ: LINK
c.l.c++ FAQ: LINK

[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Jiang
PostPosted: Sat Aug 09, 2008 2:52 am    Post subject: Re: Declaration changes meaning
       
On Aug 8, 6:31 am, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:
Quote:
Jiang wrote:
On Aug 7, 7:58 am, Bart van Ingen Schenau <b...@ingen.ddns.info
wrote:

[...]

Quote:

Both are. This code results in undefined behaviour.

If I read the code and standard correctly, no, there is no
undefined behavior here. The behavior is well defined,
that is, the standard say it explicitly that for the given code,
?no diagnostic is required?, which violates the diagnosable
rule necessary for a well-formed program. Since the code
is not well-formed, it is ill-formed.

Actually both "no diagnostic is required? and "undefined
behavior" violate diagnostic rule, thus result in ill-formed
code, but they are different. ?no diagnostic is required?
*is* well defined behavior.

You read the standard incorrectly.


Would you please help me find why it is not correct?


Quote:
The code in question (which was snipped) violates the requirement of
clause 3.3.6/2.

True.

Quote:
Clause 3.3.6/2 states that violation of the requirement does not have to
be diagnosed.

True.


Quote:
In clause 1.4/2, the standard states that if a program violates a
requirement for which no diagnosis is required, then the standard
places no restrictions on the compiler.

True.

Quote:
The definition of 'undefined behaviour' (in clause 1.3.12) is behaviour
for which the standard imposes no restrictions.

When combining these clauses, you reach the conclusion that violating
the requirement of clause 3.3.6/2 results in undefined behaviour.


But you lost me here. I do not see how you get the above result.

"Place no requirement on implementation" is the result for some
behaviors, including behaviors do not require diagnostic and the
undefined behaviors. However form this final result, I failed to
prove that the above two behaviors are equal/exchangeable.

Also we have another rule says (1.4/p1):

The set of diagnosable rules consists of all syntactic and
semantic rules in this International Standard except for
those rules containing an explicit notation that “no
diagnostic is required” or which are described as
resulting in “undefined behavior.”

so you see here the standard lists both “no diagnostic is
required” and “undefined behavior”, which shows me that
they are different.

BTW, if your above statement is true, why we need both
of them? Undefined behavior only should be sufficient in
my mind.

Quote:
And if the behaviour would be well-defined, then it must be possible to
tell what behaviour the compiler must exhibit when encountering this
construct (possibly after giving a diagnostic).

Now I am wondering my bad wording is the problem.
In my previous post, "well-defined" should be read as
"defined [well]", since in the standard, "well-defined" equals
to "well-formed". I must say I chose an wrong antonym of
"undefined behavior" in my last post. My point is,

Behaviors that "no diagnostic is required" is defined behavior,
and for such behaviors, be definition the standard places
no requirement for diagnostic.

Jiang


--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Page 1 of 3 .:. Goto page 1, 2, 3  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

Dieta Yahoo mozzila Kawały projekty garaży