Topic: Is this conversion to void* well-formed?
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/01/08 Raw View
fvali@biotrack.com writes
> Is the compiler at fault or am I not interpreting the sections
> properly or is there a section that allows such a conversion [of
> function pointers to void*] that I have skipped? or yet are these
> conversions well-formed but simply undefined? If so, what in the
> standard validates this conclusion.
Francis Glassborow wrote:
> There is no such conversions. There never have been. void * is
> explicitly a pointer to an instance of a generic data type.
> ...
> So both from the standards view and pragmatically it is an error to
> attempt to convert a function pointer to a void*.
However, it is okay to attempt to convert a function pointer to type
'void(*)()', which is about as close to 'void*' as you can get for
function pointers.
Of course, this is only true for non-member and static member
functions. Converting non-static member function pointers is
another story. (These constitute yet a third kind of pointer in
C++.) And then there's non-static member variable pointers, too.
-- David R. Tribble, dtribble@technologist.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 ]
Author: AllanW@my-dejanews.com
Date: 1999/01/04 Raw View
In article <76jc56$9gc$1@nnrp1.dejanews.com>,
fvali@biotrack.com wrote:
> Hi,
Hi!
> From my readings of certain sections (4.3, 4.10, 5.2.9, 5.2.10, 5.4)
> of the C++ standard, I found no evidence that the following
> conversions (functions pointer to void*) are well-formed:
>
> void f() { }
>
> void* pv = reinterpret_cast<void*>( /*(& optional)*/ f );
>
> pv = (void*) /*(& optional)*/ f;
This is not Standard C++.
> Yet MSVC 6.0 compiles similar conversions.
This will work on most other systems, too. Which doesn't make it
standard.
> Is the compiler at fault or am I not interpreting the sections properly
> or is there a section that allows such a conversion that I have skipped?
> or yet are these conversions well-formed but simply undefined? If so,
You're confusing two different things: What usually works, and what
the standard guarantees to work. Some of the newest items in the
standard usually don't work, but the standard guarantees that they
WILL work (when fully-conformant compilers begin to ship.) And many
legacy practices usually work, even though the standard doesn't
guarantee them.
As a practical matter, you can expect that the conversion above
PROBABLY works on any system where sizeof(&f)<=sizeof(void*).
This is not every system; on many compilers, especially in certain
modes, the size of void* might not be sufficient to hold a pointer
to any function.
As for the standard, this never guarantees that a void* can
hold a pointer to a function. The practice is not portable.
> what in the standard validates this conclusion.
The ones you pointed out, especially 5.2.10. Paragraph 1 reads
in part:
Conversions that can be performed explicitly using
reinterpret_cast are listed below. No other conversion
can be performed explicitly using reinterpret_cast.
The conversions that are listed in paragraphs 2 through 10 are:
Pointer to an integer type large enough to hold it
Integer or enumeration to pointer
Pointer-to-function to different type of pointer-to-function
Pointer-to-object to different type of pointer-to-object
Pointer-to-member to different type of pointer-to-member,
but only if both are functions or both are objects.
Lvalue T1 to reference T2, provided that T1* can convert to T2*
Nothing here allows Pointer-to-Function to Void*, so this is not
permitted.
--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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: fvali@biotrack.com
Date: 1999/01/02 Raw View
Hi,
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/01/02 Raw View
In article <76jc56$9gc$1@nnrp1.dejanews.com>, fvali@biotrack.com writes
>Is the compiler at fault or am I not interpreting the sections properly
>or is there a section that allows such a conversion that I have skipped?
>or yet are these conversions well-formed but simply undefined? If so,
>what in the standard validates this conclusion.
There is no such conversions. There never have been. void * is
explcicitly a pointer to an instance of a generic data type. In
programs using Harvard architectures (which include several program
models for the all pervasive MSDOS/Windows/Intel segmented
architectures) this is vitally important. Function pointers can (and
frequently are) larger than data pointers and so cannot be stored in
void* variables.
So both from the standards view and pragmatically it is an error to
attempt to convert a function pointer to a void*
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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 ]