Given function pointer, say void (*func)(void*), what's the best way to
display it on an iostream?
as far as I can tell, there is no default overload for operator<<, and a
function pointer doesn't have a conversion to a void*.
The only options I can see are either a C-style cast to void* (unsafe
and ugly), or some kind of template:
template<class T, class U> ostream& operator<<(ostream&, const U&)
which does some kind of copy of the pointer value into an array of
unsigned char (memcpy, or some such), and then outputs the data
byte-by-byte. This, of course, has endian issues (and formatting issues
for oddball architectures such as 16-bit intel).
Any suggestions? 7 1431
red floyd wrote:
The only options I can see are either a C-style cast to void* (unsafe and ugly),
There's nothing unsafe about it, although it's possible (but unlikely)
that it won't give you useful information. On POSIX-conformant platforms
it's well defined.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Pete Becker wrote: red floyd wrote:
The only options I can see are either a C-style cast to void* (unsafe and ugly),
There's nothing unsafe about it, although it's possible (but unlikely) that it won't give you useful information. On POSIX-conformant platforms it's well defined.
I thought the Standard said something about function pointers being cast
to void*? Hence the comment about "unsafe".
[FALLACY type=compiler-specific]
I know g++ 3.2.3 gives me a warning when I use either static_cast or
reinterpret-cast.
[/FALLACY]
red floyd wrote: Pete Becker wrote:
red floyd wrote:
The only options I can see are either a C-style cast to void* (unsafe and ugly),
There's nothing unsafe about it, although it's possible (but unlikely) that it won't give you useful information. On POSIX-conformant platforms it's well defined.
I thought the Standard said something about function pointers being cast to void*? Hence the comment about "unsafe".
Standard C++ doesn't allow converting a function pointer into a void*.
That doesn't mean it's "unsafe." It only means that if your compiler
allows it (which every one I use does) you might want to check the
compiler's documentation to see what it does. But any compiler that
doesn't do the obvious thing is seriously freaky. I know g++ 3.2.3 gives me a warning when I use either static_cast or reinterpret-cast.
Yes, that's what the standard requires. Having issued a diagnostic, the
compiler is free to do whatever the implementor chooses. If you were
writing a compiler and wanted to implement explicit conversions from
function pointers to void pointers, what would you do? (assuming the
usual architecture, where function pointers and data pointers are the
same size). There's nothing unsafe about that, is there? <g>
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Pete Becker wrote: red floyd wrote:
Pete Becker wrote:
red floyd wrote: The only options I can see are either a C-style cast to void* (unsafe and ugly),
There's nothing unsafe about it, although it's possible (but unlikely) that it won't give you useful information. On POSIX-conformant platforms it's well defined.
I thought the Standard said something about function pointers being cast to void*? Hence the comment about "unsafe".
Standard C++ doesn't allow converting a function pointer into a void*. That doesn't mean it's "unsafe." It only means that if your compiler allows it (which every one I use does) you might want to check the compiler's documentation to see what it does. But any compiler that doesn't do the obvious thing is seriously freaky.
I know g++ 3.2.3 gives me a warning when I use either static_cast or reinterpret-cast.
Yes, that's what the standard requires. Having issued a diagnostic, the compiler is free to do whatever the implementor chooses. If you were writing a compiler and wanted to implement explicit conversions from function pointers to void pointers, what would you do? (assuming the usual architecture, where function pointers and data pointers are the same size). There's nothing unsafe about that, is there? <g>
Agreed, the only place I can think of where sizeof(void*) != sizeof(void
(*)()) is in oddball stuff like '286 medium or compact model code. But
I was trying to be standard compliant. Given that the standard (in
theory) forbids such conversions, shouldn't they have provided a
portable way to output a generic function pointer?
Pete Becker wrote: [redacted]
BTW, Pete, I'm well aware of your reputation, and who you work for, and
realize your knowledge of the Standard far exceeds mine. Please don't
take offense at my arguments :-)
red floyd wrote: Agreed, the only place I can think of where sizeof(void*) != sizeof(void (*)()) is in oddball stuff like '286 medium or compact model code. But I was trying to be standard compliant.
I know. Mostly I was objecting to the word "unsafe." <g> Too many
programmers have learned, erroneously, that undefined behavior causes acne.
Given that the standard (in theory)
and in fact.
forbids such conversions,
Just to drive it in: the standard requires a diagnostic. Nothing more.
Once the compiler issues a diagnostic it can do whatever the implementor
wants.
shouldn't they have provided a portable way to output a generic function pointer?
Maybe, although it's not nearly as useful as being able to show the
address of a data object.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
red floyd wrote: Pete Becker wrote:
[redacted]
BTW, Pete, I'm well aware of your reputation, and who you work for, and realize your knowledge of the Standard far exceeds mine. Please don't take offense at my arguments :-)
I didn't take offense. Sorry if I sounded like I did. <g>
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: xsist10 |
last post by:
whats the best method. delphi lets you do something very simple
ptrMethodFoo : procedure
how do you do this in C++?
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy...
|
by: Tom |
last post by:
If I have a container class that has a map member which stores pointers
to objects that have been created via the new operator and I have a
method that returns a entry in the map, would it be best...
|
by: TS |
last post by:
I was under the assumption that if you pass an object as a param to a method
and inside that method this object is changed, the object will stay changed
when returned from the method because the...
|
by: grizggg |
last post by:
I have searched and not found an answer to this question. I ran upon
the following statement in a *.cpp file in a member function:
static const char * const pacz_HTMLContentTypeHeader =...
|
by: chutsu |
last post by:
Ok Here is a problem, I got a imaginary database program that I need
to code, to add a patient I have function inser_patient. but when I
try to input the details it doesn't quite work the way I...
|
by: Pat |
last post by:
Hi,
I've run into a strange problem, but one that seems like it might be
fairly common.
I have a single base class, from which several other classes are derived.
To keep the example simple,...
|
by: venkatagmail |
last post by:
I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:
I had to get my basics right again. I create an array and try all
possible...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |