Topic: (Correction/Clarification)


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/03/06
Raw View
Steve Clamage wrote:
>
> In article 1@excalibur.flash.net, "Christopher M. Gurnee" <gurnec@nospam.yahoo.com> writes:
> >Christopher M. Gurnee wrote in message
> ><6dftt1$ii1$1@excalibur.flash.net>...
> >>I can't find anything in the standard that requires unique functions
> >>have unique addresses.  ...
> >
> >This implies that given the overloaded function add2:
> >  int add2(int n) {return n+2;}
> >  unsigned add2(unsigned n) {return n+2;}
> >the following:
> >  static_cast<void*>((int(*)(int)) add2) ==
> >  static_cast<void*>((unsigned(*)(unsigned)) add2)
> >could evaluate to true.  (The inner C-style cast selects one of the
> >two overloads, the static_cast performs a (safe) cast to void* for
> >comparison).
>
> The draft standard in section 5.10 "Equality operators" says:
>  "Pointers to objects or functions of the same type (after pointer
>   conversions) can be compared for equality. Two pointers of the same
>   type compare equal if and only if they are both null, both point to
>   the same object or function, or both point one past the end of the
>   same array."
> The addresses of distinct functions must therefore compare unequal.
>
> Gurnee's example is invalid (and should not compile). A cast of a
> function pointer to a void* is not only not safe, it has no defined
> semantics. In particular, a static_cast cannot be used to perform that
> conversion.

I've now found a valid example:

template<class T> void f() {}

void (*f1)() = f<int>;
void (*f2)() = f<double>;

assert(f1 != f2);

In this example the functions are both the same type, void(),
therefore comparision is allowed. And therefore the functions
must have different adresses, despite being ideal candidates
for the "two-in-one" optimisation.
---
[ 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: "Christopher M. Gurnee" <gurnec@nospam.yahoo.com>
Date: 1998/03/04
Raw View
Christopher M. Gurnee wrote in message
<6dftt1$ii1$1@excalibur.flash.net>...
>I can't find anything in the standard that requires unique functions
>have unique addresses.  Although each object must have a unique

This implies that given the overloaded function add2:
  int add2(int n) {return n+2;}
  unsigned add2(unsigned n) {return n+2;}
the following:
  static_cast<void*>((int(*)(int)) add2) ==
  static_cast<void*>((unsigned(*)(unsigned)) add2)
could evaluate to true.  (The inner C-style cast selects one of the
two overloads, the static_cast performs a (safe) cast to void* for
comparison).

This could also be true given two differently-named functions
(possibly with different signatures), or given two template
specializations.

>function1: jmp function4
>function2: jmp function4
>function3: nop
>function4: nop
>function5:
>  Instructions for all functions here.

The first two lines should have read:
function1: jmp function5
function2: jmp function5
(doesn't really change anything, it was just a minor oversight :P )

-Chris
---
[ 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: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/03/04
Raw View
Christopher M. Gurnee wrote:
>
> Christopher M. Gurnee wrote in message
> <6dftt1$ii1$1@excalibur.flash.net>...
> >I can't find anything in the standard that requires unique functions
> >have unique addresses.  Although each object must have a unique
>
> This implies that given the overloaded function add2:
>   int add2(int n) {return n+2;}
>   unsigned add2(unsigned n) {return n+2;}
> the following:
>   static_cast<void*>((int(*)(int)) add2) ==
>   static_cast<void*>((unsigned(*)(unsigned)) add2)
> could evaluate to true.

I don't think you can convert a pointer to function to a
pointer to void and expect the result to have any interresting
meaning. Pointers to void are only usefull for storing pointers
to objects.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ 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: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/03/04
Raw View
In article 1@excalibur.flash.net, "Christopher M. Gurnee" <gurnec@nospam.yahoo.com> writes:
>Christopher M. Gurnee wrote in message
><6dftt1$ii1$1@excalibur.flash.net>...
>>I can't find anything in the standard that requires unique functions
>>have unique addresses.  ...
>
>This implies that given the overloaded function add2:
>  int add2(int n) {return n+2;}
>  unsigned add2(unsigned n) {return n+2;}
>the following:
>  static_cast<void*>((int(*)(int)) add2) ==
>  static_cast<void*>((unsigned(*)(unsigned)) add2)
>could evaluate to true.  (The inner C-style cast selects one of the
>two overloads, the static_cast performs a (safe) cast to void* for
>comparison).

The draft standard in section 5.10 "Equality operators" says:
 "Pointers to objects or functions of the same type (after pointer
  conversions) can be compared for equality. Two pointers of the same
  type compare equal if and only if they are both null, both point to
  the same object or function, or both point one past the end of the
  same array."
The addresses of distinct functions must therefore compare unequal.

Gurnee's example is invalid (and should not compile). A cast of a
function pointer to a void* is not only not safe, it has no defined
semantics. In particular, a static_cast cannot be used to perform that
conversion.

---
Steve Clamage, stephen.clamage@sun.com
---
[ 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              ]