Topic: C++ AMBIGUITY??


Author: "Christopher M. Gurnee" <gurnec@nospam.yahoo.com>
Date: 1998/06/19
Raw View
jkanze@otelo.ibmmail.com wrote in message
<6m6gfi$4nh$1@nnrp1.dejanews.com>...
>In article <35854804.13A8B0ED@bell.ca>,
>  "KNAPEN, GREGORY" <gregory.knapen@bell.ca> wrote:
[snip]
>>
>> Thanks for the pointer, what I was looking for was in section 8.2.
>>
>> In the standard draft, there is this example:
>>
>> class C { };
>>
>> void f(int(C)) {}
>>
>> // this actually means void f(int (*fp)(C c)){} !!
>> // not void f(int C);
>
>I found the example, but I don't understand it.  How can something
>like int(C) possibly be interpreted as a pointer to function?  (What
>rule allows a declaration of a pointer to function to not contain a
*?)

None.  The parameter-declaration "int(C)" specifies an unnamed
parameter of type "function of (C) returning int" instead of a
parameter named "C" of type "int"  (CD2 8.2/7).

Next, recall that any parameter of type "function returning T" is
adjusted to be "pointer to function returning T" (CD2 8.3.5/3).  This
is similar to the "array of T" to "pointer to T" adjustment.

As an example, the parameter-declarations below declare different
types, but the function declarations both declare a single
(non-overloaded) function foo with one parameter of the type "pointer
to function taking no parameter returning int."
void foo(int    ());
void foo(int (*)());


-Chris Gurnee





      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]





Author: "Christopher M. Gurnee" <gurnec@nospam.yahoo.com>
Date: 1998/06/18
Raw View
[I apologize if this is a repost, but my first post didn't seem to get
through -Chris]

jkanze@otelo.ibmmail.com wrote in message
<6m6gfi$4nh$1@nnrp1.dejanews.com>...
>In article <35854804.13A8B0ED@bell.ca>,
>  "KNAPEN, GREGORY" <gregory.knapen@bell.ca> wrote:
[snip]
>>
>> Thanks for the pointer, what I was looking for was in section 8.2.
>>
>> In the standard draft, there is this example:
>>
>> class C { };
>>
>> void f(int(C)) {}
>>
>> // this actually means void f(int (*fp)(C c)){} !!
>> // not void f(int C);
>
>I found the example, but I don't understand it.  How can something
>like int(C) possibly be interpreted as a pointer to function?  (What
>rule allows a declaration of a pointer to function to not contain a
*?)

None.  The parameter-declaration "int(C)" specifies an unnamed
parameter of type "function of (C) returning int" instead of a
parameter named "C" of type "int"  (CD2 8.2/7).

Next, recall that any parameter of type "function returning T" is
adjusted to be "pointer to function returning T" (CD2 8.3.5/3).  This
is similar to the "array of T" to "pointer to T" adjustment.

As an example, the parameter-declarations below declare different
types, but the function declarations both declare a single
(non-overloaded) function foo with one parameter of the type "pointer
to function taking no parameter returning int."
void foo(int    ());
void foo(int (*)());


-Chris Gurnee



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1998/06/18
Raw View
jkanze@otelo.ibmmail.com wrote in message
<6m6gfi$4nh$1@nnrp1.dejanews.com>...
>In article <35854804.13A8B0ED@bell.ca>,
>  "KNAPEN, GREGORY" <gregory.knapen@bell.ca> wrote:
>> In the standard draft, there is this example:
>>
>> class C { };
>>
>> void f(int(C)) {}
>>
>> // this actually means void f(int (*fp)(C c)){} !!
>> // not void f(int C);
>
>I found the example, but I don't understand it.  How can something
>like int(C) possibly be interpreted as a pointer to function?  (What
>rule allows a declaration of a pointer to function to not contain a *?)


Section 8.3.5.3 of CD2 says

    The type of a function is determined using the following  rules.
    The  type  of  each parameter is determined from its own
    decl-specifier-seq and declarator.  After determining the type of each
    parameter,  any  parameter of type "array of T" or "function returning
    T" is adjusted to be "pointer to T" or "pointer to function  returning
    T,"  respectively.

That sounds like "void f( int fp() )" is the same as "void f( int
(*fp)() )".  That makes sense to me.  In most cases, you can use a
declaration of a function as a synonym for a declaration of a pointer to a
function.  This is just one more of those cases.

Now the identifier names for the parameters are optional.  So if you remove
the optional parameters, and remove the redundant "(*)", you get to the
example cited in the standard.

//these declarations are the same
void f(int (*fp)(C c));
void f(int (*)(C));  //remove optional identifiers "fp" and "c"
void f(int(C));       //remove redundant "(*)"

//or alternatively
void f(int (*fp)(C c));
void f(int fp(C c));     //remove redundant "(*)"
void f(int(C));            //remove optional identifiers "fp" and "c"



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/06/18
Raw View
jkanze@otelo.ibmmail.com wrote:
>
> In article <35854804.13A8B0ED@bell.ca>,
>   "KNAPEN, GREGORY" <gregory.knapen@bell.ca> wrote:
> >
> > In the standard draft, there is this example:
> >
> > class C { };
> >
> > void f(int(C)) {}
> >
> > // this actually means void f(int (*fp)(C c)){} !!
> > // not void f(int C);
>
> I found the example, but I don't understand it.  How can something
> like int(C) possibly be interpreted as a pointer to function?  (What
> rule allows a declaration of a pointer to function to not contain a
> *?)

CD2 8.3.5 para 3: After determining the type of each parameter, any
parameter of type "array of T" or "function returning T" is adjusted to
be "pointer to T" or "pointer to function  returning T," respectively.

And since you can always leave parameters unnamed, int(*fp)(C c) reduces
to int(*)(C), which reduces to int(C). (Sort of like pronouncing French,
in which about 30% of the letters are silent!)

--

Ciao,
Paul


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "KNAPEN, GREGORY" <gregory.knapen@bell.ca>
Date: 1998/06/12
Raw View
Hi,

I'm trying to understand the c++ grammar in the draft standard (or from
the C++ Programming Language 3rd edition).

There is an ambiguity in the language between a function declaration and
a variable declaration with and initializer.  This ambiguity is resolved
purely syntactically.

For example:
assume T is a type:

T t((T)a);

This is considered a var declaration( a var t of type T initialized with
a cast to T).

the following is a function definition

T t(T(a));

t is a function and a is an argument of type T


But what if I have this construct

int t(T(T(T(a))));

Now my compiler still considers this a function declaration but I am not
sure what it means.

My compiler interprets it as
int t(class T(*)(class T(*)(class T)))

I guess this means that t takes a pointer to a function that returns a T
and that takes a pointer to a function that takes a T and returns a T.
Right?

Now this is probably the only reasonnable interpretation for this
construct.  But how does the compiler make this interpretation. I mean
are there rules in the standard to make such assumptions.  If so, can
some explain these to me, and maybe tell me where in the standard this
is?

Thanks,

Greg Knapen



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: lfischer@netcom.com (Leonard Fischer)
Date: 1998/06/15
Raw View
I don't know if this is exactly what you're talking about, but it sounds
very close.  See section 6.8, Ambiguity resolution, on page 99
regarding ambiguity involving expression-statements and declarations.
Another variation is addressed in section 8.2, page 128.  Both
references are to the FDIS Reference Number ISO/IEC FDIS 14882:1998(E)
with cover sheet dated 29 September 1997.

Len

KNAPEN, GREGORY (gregory.knapen@bell.ca) wrote:
: Hi,

: I'm trying to understand the c++ grammar in the draft standard (or from
: the C++ Programming Language 3rd edition).

: There is an ambiguity in the language between a function declaration and
: a variable declaration with and initializer.  This ambiguity is resolved
: purely syntactically.

: For example:
: assume T is a type:

: T t((T)a);

: This is considered a var declaration( a var t of type T initialized with
: a cast to T).

: the following is a function definition

: T t(T(a));

: t is a function and a is an argument of type T


: But what if I have this construct

: int t(T(T(T(a))));

: Now my compiler still considers this a function declaration but I am not
: sure what it means.

: My compiler interprets it as
: int t(class T(*)(class T(*)(class T)))

: I guess this means that t takes a pointer to a function that returns a T
: and that takes a pointer to a function that takes a T and returns a T.
: Right?

: Now this is probably the only reasonnable interpretation for this
: construct.  But how does the compiler make this interpretation. I mean
: are there rules in the standard to make such assumptions.  If so, can
: some explain these to me, and maybe tell me where in the standard this
: is?

: Thanks,

: Greg Knapen


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "KNAPEN, GREGORY" <gregory.knapen@bell.ca>
Date: 1998/06/15
Raw View
Leonard Fischer wrote:
>
> I don't know if this is exactly what you're talking about, but it sounds
> very close.  See section 6.8, Ambiguity resolution, on page 99
> regarding ambiguity involving expression-statements and declarations.
> Another variation is addressed in section 8.2, page 128.  Both
> references are to the FDIS Reference Number ISO/IEC FDIS 14882:1998(E)
> with cover sheet dated 29 September 1997.
>
> Len
>

Thanks for the pointer, what I was looking for was in section 8.2.

In the standard draft, there is this example:

class C { };

void f(int(C)) {}

// this actually means void f(int (*fp)(C c)){} !!
// not void f(int C);

This means that compilers have to check if C is the name of a type or
not.  Shouln't this case be an error.  I mean wouldn't it be better, if
instead of trying to resolve ambiguous cases such as this, the compiler
could report an error like "ambiguous construct, C is also a type". And
if you want to declare function pointers, maybe you should do so
explicitly. Maybe this would keep certain programs more readable.


Greg Knapen



[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]