Topic: legality of this code


Author: benoit_hudson@my-deja.com
Date: 09 Dec 00 19:36:39 GMT
Raw View
> > As for assigning a pointer-to-const-qualified-member-function to a
> > pointer-to-non-cv-qualified-member-function, I can't find anything
in the
> > standard that says either way.  From the definition of 9.3.2/4 I
would
> > assume it to be analogous: that a conversion such that
> >
> > (X::*) pmf1 (T1 p1, T2 p2, T3 p3) cv1
> >
> > is the destination and
> >
> > (X::*) pmf2 (T1 p1, T2 p2, T3 p3) cv2

Come to think of it, isn't the function signature:
 pmf1(cv1 X*, T1 p1, etc)
 pmf2(cv2 X*, T1 p1, etc)

So standard rules for function-pointer matching based on those
signatures should take place...

> There is no standard conversion in the language that does conversions
> among pointers to functions or pointers to member functions of
different types.

... which is to say that the compiler should issue a diagnostic if we
try (rather than do something reasonable, but non-standard).  Indeed,
both my compilers complain about
 void f(const char*);
 void (*fptr)(char*) = &f;
and vice-versa with the 'const'.

 -- Benoit


Sent via Deja.com http://www.deja.com/
Before you buy.

      [ 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: "Mike Dimmick" <mike@dimmick.demon.co.uk>
Date: 06 Dec 00 01:24:04 GMT
Raw View
[Cross-posted to comp.std.c++ by the current poster, who isn't yet terribly
good at finding his way around the standard]

"Benoit Hudson" <bh@techhouse.brown.edu> wrote in message
news:903u84$jvq@cocoa.brown.edu...
> Can someone tell me if this code is legal?  VC6.0 and Sun CC 4.2 reject
it,
> gcc 2.95.2 accepts it.

[...]

> The older compilers (VC6.0 and Sun CC 4.2) just complain.  gcc 2.95.2 digs
> out the extra const.
>
> Either way seems to make sense: can't reduce the number of cv-quals, so
> it's illegal.  Or, A::compare is allowed to change T, so a function that
> promises not to change bar is a fine match.

The standard says, in 9.3.2/4:

"A cv-qualified member function can be called on an object-expression
(5.2.5) only if the object-expression is as cv-qualified or
less-cv-qualified than the member function."

As for assigning a pointer-to-const-qualified-member-function to a
pointer-to-non-cv-qualified-member-function, I can't find anything in the
standard that says either way.  From the definition of 9.3.2/4 I would
assume it to be analogous: that a conversion such that

(X::*) pmf1 (T1 p1, T2 p2, T3 p3) cv1

is the destination and

(X::*) pmf2 (T1 p1, T2 p2, T3 p3) cv2

is the source should be permitted so long as cv2 "is as cv-qualified or
less-cv-qualified" than cv1.  This ought to be a general rule for function
pointers.

Questions?  Comments?

--
Mike Dimmick







      [ 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: Andrei Iltchenko <Andrei.Iltchenko@openmarket.com>
Date: 2000/12/06
Raw View
> As for assigning a pointer-to-const-qualified-member-function to a
> pointer-to-non-cv-qualified-member-function, I can't find anything in the
> standard that says either way.  From the definition of 9.3.2/4 I would
> assume it to be analogous: that a conversion such that
>
> (X::*) pmf1 (T1 p1, T2 p2, T3 p3) cv1
>
> is the destination and
>
> (X::*) pmf2 (T1 p1, T2 p2, T3 p3) cv2
>
> is the source should be permitted so long as cv2 "is as cv-qualified or
> less-cv-qualified" than cv1.  This ought to be a general rule for function
> pointers.
>
> Questions?  Comments?

There is no standard conversion in the language that does conversions
among pointers to functions or pointers to member functions of different types.

You can only convert a pointer-to-const-qualified-member-function to a
pointer-to-non-cv-qualified-member-function using an explicit type conversion
of the form reinterpret_cast<type-id>(expression). And the standard says about
such conversions that they are unspecified (see 5.2.10/9), i.e might, or might
not,
produce a representation different from the original value. In other words,
the standard doesn't guarantee that after assigning (using an reinterpret_cast)

of an expression pmf2 of type: TR (X::*) (T1 p1, T2 p2, T3 p3) cv2
to
an l-value pmf1 of type: TR (X::*) (T1 p1, T2 p2, T3 p3) cv1
where the types TR, X, T1, T2, and T3 are the same types in both declarations,
and cv1 is more cv-qualified than cv2

pmf1 will point to the same function as pmf2.

P.S. expressions:
simple-type-specifier(expression-listopt) and (type-id) cast-expression
may also be used to do conversions among pointers to function or pointers
to member functions of different types, the result will be the same as in
the case of reinterpret_cast, since the same semantic restriction and
behaviours will apply.


Regards,
Andrei Iltchenko.
---
[ 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                ]