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<<(ostr eam&, 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 1449
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 *enemydata; // Holds information about an enemy (in a game)
// Its a double linked list node
|
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 to return the
entry as a dereferenced pointer so it becomes a class or return a
reference to the dereferenced class?
I believe that it would be best to dereference the class pointer
retrieved from the map and then send it back as a reference to...
|
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 object is a reference type?
my code is not proving that. I have a web project i created from a web
service that is my object:
public class ExcelService : SoapHttpClientProtocol
{
|
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 = "Content-Type:
text/html\r\n";
Why is the second const needed and what does it do?
Thanks
| |
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 wanted it to.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
|
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, the base class is "Animal" and the derived
classes are "Cat," "Dog," and "Horse."
The base class has a pure virtual method:
|
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 ways of passing an array. In the following code, fun1(int
a1) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |