Topic: Defect Report: Multiple interpretations possible of ambiguity resolution for declarations
Author: "Anthony Williams"<anthwil@nortelnetworks.com>
Date: Fri, 1 Mar 2002 17:05:11 GMT Raw View
"B. van Ingen Schenau" <bvis@universalmail.com> wrote in message
news:k48q7u0tlbhqd4n35uo5guk63nvk913ouh@4ax.com...
> [Moderator's note: this defect report has been
> forwarded to the C++ committee. -moderator.]
> Consider the following program:
> struct Point
> {
> Point(int){}
> };
> struct Lattice
> {
> Lattice(Point, Point, int){}
> };
> int main(void)
> {
> int a, b;
> Lattice latt(Point(a), Point(b), 3); /* Line X */
> }
> The problem concerns the line marked /* Line X */, which is an
> ambiguous declarations for either an object or a function.
Line X cannot be interpreted as a function declaration, because "3" is not a
valid declarator, as identifiers must start with a nondigit, so cannot be a
parameter-declaration, which means that it cannot form part of a
parameter-declaration-list, or parameter-declaration-clause. Therefore the
declarator syntax for functions:
direct-declarator (parameter-declaration-clause) cv-qualifier-seq-opt
exception-specification-opt
cannot be met, and there is no ambiguity --- the declaration must be an
object declaration.
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "B. van Ingen Schenau" <bvisNOSPAM@universalmail.com>
Date: Fri, 1 Mar 2002 23:19:08 GMT Raw View
On Fri, 1 Mar 2002 17:05:11 GMT, "Anthony
Williams"<anthwil@nortelnetworks.com> wrote:
>"B. van Ingen Schenau" <bvis@universalmail.com> wrote in message
>news:k48q7u0tlbhqd4n35uo5guk63nvk913ouh@4ax.com...
>> [Moderator's note: this defect report has been
>> forwarded to the C++ committee. -moderator.]
>
>> Consider the following program:
>
>> struct Point
>> {
>> Point(int){}
>> };
>
>> struct Lattice
>> {
>> Lattice(Point, Point, int){}
>> };
>
>> int main(void)
>> {
>> int a, b;
>> Lattice latt(Point(a), Point(b), 3); /* Line X */
>> }
>
>> The problem concerns the line marked /* Line X */, which is an
>> ambiguous declarations for either an object or a function.
>
>Line X cannot be interpreted as a function declaration, because "3" is not a
>valid declarator, as identifiers must start with a nondigit, so cannot be a
>parameter-declaration, which means that it cannot form part of a
>parameter-declaration-list, or parameter-declaration-clause. Therefore the
>declarator syntax for functions:
>
>direct-declarator (parameter-declaration-clause) cv-qualifier-seq-opt
>exception-specification-opt
>
>cannot be met, and there is no ambiguity --- the declaration must be an
>object declaration.
But clause 8.2/1 says:
[...] Just as for the ambiguities mentioned in 6.8, the resolution is
to consider any construct that could possibly be a declaration a
declaration.[...]
This part can be read such that, because the constructs "Point(a)" and
"Point(b)" in the declaration above can be interpreted as declarations
themselves (declaration of function parameter), they must be
interpreted as declarations.
This reading of the clause then leads to the conclusion that the
entire declaration in Line X is malformed.
>
>Anthony
Bart v Ingen Schenau
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams"<anthwil@nortelnetworks.com>
Date: Mon, 4 Mar 2002 17:07:17 GMT Raw View
"B. van Ingen Schenau" <bvisNOSPAM@universalmail.com> wrote in message
news:amgv7u8vmmb9ksa7grvc1s2fscv61d2fdg@4ax.com...
> On Fri, 1 Mar 2002 17:05:11 GMT, "Anthony
> Williams"<anthwil@nortelnetworks.com> wrote:
> >"B. van Ingen Schenau" <bvis@universalmail.com> wrote in message
> >news:k48q7u0tlbhqd4n35uo5guk63nvk913ouh@4ax.com...
> >> struct Point
> >> {
> >> Point(int){}
> >> };
> >> struct Lattice
> >> {
> >> Lattice(Point, Point, int){}
> >> };
> >> int main(void)
> >> {
> >> int a, b;
> >> Lattice latt(Point(a), Point(b), 3); /* Line X */
> >> }
> >> The problem concerns the line marked /* Line X */, which is an
> >> ambiguous declarations for either an object or a function.
> >Line X cannot be interpreted as a function declaration, because "3" is
not a
> >valid declarator
> But clause 8.2/1 says:
> [...] Just as for the ambiguities mentioned in 6.8, the resolution is
> to consider any construct that could possibly be a declaration a
> declaration.[...]
> This part can be read such that, because the constructs "Point(a)" and
> "Point(b)" in the declaration above can be interpreted as declarations
> themselves (declaration of function parameter), they must be
> interpreted as declarations.
> This reading of the clause then leads to the conclusion that the
> entire declaration in Line X is malformed.
This is in the context of function declaration vs object declaration (which
is also a definition). The expression "Point(a)" _within_ the declaration of
latt _cannot_ be interpreted as a declaration, because it is not a complete
statement. On its own,
Point(a);
it would be a declaration (per 6.8), but as part of a larger expression it
is not. The entire line cannot be interpreted as a function declaration
either, for the reasons I decsribed in my first post, so this must be an
object declaration and definition.
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "B. van Ingen Schenau" <bvis@universalmail.com>
Date: Mon, 4 Mar 2002 20:21:23 GMT Raw View
On Mon, 4 Mar 2002 17:07:17 GMT, "Anthony
Williams"<anthwil@nortelnetworks.com> wrote:
>"B. van Ingen Schenau" <bvisNOSPAM@universalmail.com> wrote in message
>news:amgv7u8vmmb9ksa7grvc1s2fscv61d2fdg@4ax.com...
>> On Fri, 1 Mar 2002 17:05:11 GMT, "Anthony
>> Williams"<anthwil@nortelnetworks.com> wrote:
>
>> >"B. van Ingen Schenau" <bvis@universalmail.com> wrote in message
>> >news:k48q7u0tlbhqd4n35uo5guk63nvk913ouh@4ax.com...
>> >> struct Point
>> >> {
>> >> Point(int){}
>> >> };
>
>> >> struct Lattice
>> >> {
>> >> Lattice(Point, Point, int){}
>> >> };
>
>> >> int main(void)
>> >> {
>> >> int a, b;
>> >> Lattice latt(Point(a), Point(b), 3); /* Line X */
>> >> }
>
>> >> The problem concerns the line marked /* Line X */, which is an
>> >> ambiguous declarations for either an object or a function.
>
>> >Line X cannot be interpreted as a function declaration, because "3" i=
s
>not a
>> >valid declarator
>
>> But clause 8.2/1 says:
>> [...] Just as for the ambiguities mentioned in 6.8, the resolution is
>> to consider any construct that could possibly be a declaration a
>> declaration.[...]
>
>> This part can be read such that, because the constructs "Point(a)" and
>> "Point(b)" in the declaration above can be interpreted as declarations
>> themselves (declaration of function parameter), they must be
>> interpreted as declarations.
>> This reading of the clause then leads to the conclusion that the
>> entire declaration in Line X is malformed.
>
>This is in the context of function declaration vs object declaration (wh=
ich
>is also a definition). The expression "Point(a)" _within_ the declaratio=
n of
>latt _cannot_ be interpreted as a declaration, because it is not a compl=
ete
>statement. On its own,
>
>Point(a);
>
>it would be a declaration (per 6.8), but as part of a larger expression =
it
>is not. The entire line cannot be interpreted as a function declaration
>either, for the reasons I decsribed in my first post, so this must be an
>object declaration and definition.
I can't do anything but agree with your argumentation.
So there is only one correct interpretation of clause 8.2/1, but I
have to say that with some rewording, the clause can be made a lot
clearer, like stating explicitly that the entire declaration must be
taken into account and that function declarations are preferred over
object declarations.
I would like to suggest the following as replacement for the current
clause 8.2/1:
The ambiguity arising from the similarity between a functionstyle
cast and a declaration mentioned in 6.8 can also occur in the context
of a declaration. In that context, the choice is between a function
declaration with a redundant set of parentheses around a parameter
name and an object declaration with a functionstyle cast as the
initializer. The resolution is to consider any construct that could
possibly be a function declaration a function declaration. [Note: To
disambiguate, the whole declaration might have to be examined to
determine if it is an object or a function declaration.] [Note: a
declaration can be explicitly disambiguated by a nonfunctionstyle
cast, by a =3D to indicate initialization or by removing the redundant
parentheses around the parameter name. ] [Example:
struct S {
S(int);
};
void foo(double a)
{
S w(int(a)); // function declaration
S x(int()); // function declaration
S y((int)a); // object declaration
S z =3D int(a); // object declaration
}
=97end example]
>
>Anthony
Bart v Ingen Schenau
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "B. van Ingen Schenau" <bvis@universalmail.com>
Date: 01 Mar 02 07:11:57 GMT Raw View
[Moderator's note: this defect report has been
forwarded to the C++ committee. -moderator.]
Consider the following program:
struct Point
{
Point(int){}
};
struct Lattice
{
Lattice(Point, Point, int){}
};
int main(void)
{
int a, b;
Lattice latt(Point(a), Point(b), 3); /* Line X */
}
The problem concerns the line marked /* Line X */, which is an
ambiguous declarations for either an object or a function.
The clause that governs this ambiguity is 8.2/1, and reads:
[dcl.ambig.res] 8.2 Ambiguity resolution
1 The ambiguity arising from the similarity between a function style
cast and a declaration mentioned in 6.8 can also occur in the context
of a declaration. In that context, the choice is between a function
declaration with a redundant set of parentheses around a parameter
name and an object declaration with a function-style cast as the
initializer. Just as for the ambiguities mentioned in 6.8, the
resolution is to consider any construct that could possibly be a
declaration a declaration. [Note: a declaration can be explicitly
disambiguated by a nonfunction style cast, by a = to indicate
initialization or by removing the redundant parentheses around the
parameter name. ] [Example:
struct S {
S(int);
};
void foo(double a)
{
S w(int(a)); // function declaration
S x(int()); // function declaration
S y((int)a); // object declaration
S z = int(a); // object declaration
}
-- end example]
Based on this clause there are two possible interpretations of the
declaration in line X:
1 The declaration of latt declares a function with a return value of
the type Lattice and takin three arguments. The type of the first two
arguments is Point and each of these arguments is followed by a
parameter name in redundant parentheses. The type of the third
argument can not be determined, because it is a literal.
This will result in a syntax error.
2 The declaration of latt declares an object, because the other option
(a function declaration) would result in a syntax error.
Note that the last sentence before the [Note: is not much help,
because both options are declarations.
Bart v Ingen Schenau
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]