![]() |
|
|
Welcome to the { mindfrost82.com } forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
rkldabs@gmail.com ha scritto:
> 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. > a.1 which compiler is right? Both, although the fact the code compiles in VS2008 is deplorable. > 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 ;) > 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 http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
rkldabs@gmail.com wrote:
> 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. > a.2 Does an ill-formed program always require to generate > compilation error? No. Especially not if the rule says "no diagnostic is required". > 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 http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
rkldabs@gmail.com wrote:
> 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. > > a.1 which compiler is right? Both are. This code results in undefined behaviour. > 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". > 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. > > Regards, > Dabs > Bart v Ingen Schenau -- a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq c.l.c FAQ: http://c-faq.com/ c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/ [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
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. Jiang -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
On Aug 7, 4:46 am, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote: > rkld...@gmail.com ha scritto: [...] > > > 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) ] > 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 http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
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. 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 http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
On Aug 8, 12:58 am, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote: > 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. > 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 ;) " 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? > 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. > 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 http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
Jiang wrote:
> 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). > > Jiang > Bart v Ingen Schenau -- a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq c.l.c FAQ: http://c-faq.com/ c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/ [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Declaration changes meaning
On Aug 8, 6:31 am, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote: > Jiang wrote: > > On Aug 7, 7:58 am, Bart van Ingen Schenau <b...@ingen.ddns.info> > > wrote: [...] > > >> 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? > The code in question (which was snipped) violates the requirement of > clause 3.3.6/2. True. > Clause 3.3.6/2 states that violation of the requirement does not have to > be diagnosed. True. > 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. > 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. > 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 http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|