C++ Comparing Member Function Pointers - c++

In C++, is it possible to define a sort order for pointers to member functions? It seems that the operator< is undefined. Also, it's illegal to cast to void*.
class A
{
public:
void Test1(){}
void Test2(){}
};
int main()
{
void (A::* const one)() = &A::Test1;
void (A::* const two)() = &A::Test2;
bool equal = one == two; //Equality works fine.
bool less = one < two; //Less than doesn't.
return 0;
}
Thanks!

Function pointers are not relationally comparable in C++. Equality comparisons are supported, except for situations when at least one of the pointers actually points to a virtual member function (in which case the result is unspecified).
Of course, you can always introduce an ordering by implementing a comparison predicate and comparing the pointers explicitly (won't look too elegant though, since you can only use equality comparisons). Other possible solutions would cross into the territory of the various implementation-specific "hacks".

Member function pointers are not actual pointers. You should look at them as opaque structs. What does a method pointer contain:
struct method_pointer {
bool method_is_virtual;
union {
unsigned vtable_offset; // for a virtual function, need the vtable entry
void* function_pointer; // otherwise need the pointer to the concrete method
}
};
If you could cast this to void* (you can't) all you would have is a pointer the the struct, not a pointer to code. That's why operator<() is undefined as well since the value of the struct's pointer is just where ever it happens to be in memory.
In addition to that, what are you sorting by?

Related

How to cast void pointers to function pointers

I am trying to write a class that will be appended with some function pointers.
This pointers will be called from another method of this class. I will be storing the function pointers on a void* vector so anything can go on a single vector, instead of a different vector for each type.
I intend to declare a different AppendCallback methods for any different function I need to call from inside the class, for example:
void MyClass:AppendCallback(void (*Callback)())
{
_CallbackVector.push_back((void*)Callback);
_IdVector.push_back(VoidID);
}
void MyClass:AppendCallback(void (*Callback)(uint32_t));
void MyClass:AppendCallback(void (*MyOtherClass::Callback)());
void MyClass:AppendCallback(void (*MyOtherClass::Callback)(uint32_t));
There will be a second vector that only contains identifiers to know what the void* points to, this is going to be assigned also on the AppendCallback Methods.
How can I cast the void pointer again to the function pointers for calling those functions?
Maybe something like this?
void MyClass::Service(uint32_t x)
{
for(uint i = 0; i < _CallbackVector.size(); i++)
{
switch(_IdVector[i])
{
case VoidID: void(*_CallbackVector[i]()); break;
case Uint32ID: void(*_CallbackVector[i](x)); break;
}
}
}
Edit:
Is this a proper way of casting from a void* to a function pointer?
That's not allowed in C++:
Converting a void* to a function pointer directly is not allowed (should not compile using any of the casts) in C++98/03. It is conditionally supported in C++0x (an implementation may choose to define the behavior and if it does define it then it must do what the standard says it should do. A void*, as defined by the C++98/03 standard, was meant to point to objects and not to contain function pointers or member pointers.
And again:
You can't.
You can cast a data pointer to void* and then back to the same pointer type you have started with. std::function is not a pointer type, so the cast is statically invalid, and it's not the same thing you have started with. You have started with a .target of type void()() but it's not a data pointer, it's a function pointer, so casting it to void and back is implementation-defined.
In your specific situation, you could try something like this:
class PointerToFunction {
};
template <typename Type>
class PointerToFunctionType : public PointerToFunction, std::function<Type> {
public:
using std::function<Type>::function; // I think this is the right syntax for this trick: https://softwareengineering.stackexchange.com/a/197901/342247
};
// Now, we can do this:
std::vector<PointerToType*> _CallbackVector;
Basically, we're using an inheritance trick similar to how boost::any/std::any (since C++17) is implemented to store any std::function in a pointer. The only issue is knowing how to convert it back. Doing this properly would depend on how you expected to convert that void* back to begin with (i.e., knowing its type), so I'll leave that part up to you.

How to get numeric data from a member function pointer for a hash? [duplicate]

How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function?
Example:
I have several bool (Class::*functionPointer)() (not static) that point to several diferent methods of the class Class and i need to hash those pointer-to-member-function.
How can i do that?
Also how can i compare (std::less) those member function pointers so i can store them in a std::set?
All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So you could try:
bool (Class::*fn_ptr)() = &Class::whatever;
const char *ptrptr = static_cast<const char*>(static_cast<const void*>(&fn_ptr));
Now treat ptrptr as pointing to an array of (sizeof(bool (Class::*)())) bytes, and hash or compare those bytes. You can use unsigned char instead of char if you prefer.
This guarantees no false positives - in C++03, pointers to member functions are POD, which means among other things that they can be copied using memcpy. This implies that if have the same byte-for-byte values, then they are the same.
The problem is that the storage representation of member function pointers could include bits which do not participate in the value - so they will not necessarily be the same for different pointers to the same member function. Or the compiler might, for some obscure reason, have more than one way of pointing to the same function of the same class, which are not byte-wise equal. Either way you can get false negatives. You'll have to look into how member function pointers actually work on your implementation. It must implement operator== for member function pointers somehow, and if you can find out how then you can probably figure out an order and a hash function.
That's potentially hard: member function pointers are awkward, and the storage is likely to include different amounts of non-participating "slack space" according to what kind of function is pointed to (virtual, inherited). So you'll probably have to interact quite significantly with your compiler's implementation details. This article might help get you started: http://www.codeproject.com/KB/cpp/FastDelegate.aspx
A cleaner alternative might be to do a linear search through an array in order to "canonicalise" all your function pointers, then compare and hash based on the position of the "canonical" instance of that function pointer in your array. Depends what your performance requirements are. And even if there are requirements, does the class (and its derived classes) have so many functions that the linear search will take that long?
typedef bool (Class::*func)();
vector<func> canon;
size_t getIndexOf(func fn_ptr) {
vector<func>::iterator it = find(canon.begin(), canon.end(), fn_ptr);
if (it != canon.end()) return it - canon.begin();
canon.push_back(func);
return canon.size() - 1;
}
I could not cast the pointer (in Microsoft compiler 2010)as described in previous answer but this works for me:
static string fmptostr(int atype::*opt)
{
char buf[sizeof(opt)];
memcpy(&buf,&opt,sizeof(opt));
return string(buf,sizeof(opt));
}
About bitwise identity of the pointer, it can be bitwise so it seems if appropriate compiler switches are used. At least this is true for Microsoft compiler E.g
using #pragma pointers_to_members
and a switch.../vmg
If your member function pointer is unique, which is true in most of cases for callback-based subscriptions, then you can use the tick with type_index, which uniqueness is guaranteed by uniqueness of type (i.e. Class::Method) in your program, and it is suitable to be stored in unordered_map, i.e.
struct MyEvent {
using fn_t = std::function<void(MyEvent &)>;
using map_t = std::unordered_map<std::type_index, fn_t>;
template <typename Handler>
void subscribe(Object& obj, Handler&& handler) {
fn_t fn = [&, handler = std::move(handler)](MyEvent& event) {
(obj.*handler)(event);
}
std::type_index index = typeid(Handler);
subscribers.emplace(std::move(index), std::move(fn));
}
void fire() {
for(auto& pair: subscribers) {
auto& fn = pair.second;
fn(*this);
}
}
map_t subscribers;
}
And the subscription and fire event example:
MyEvent event;
MyObject obj = ...;
event.subscribe(obj, &MyObject::on_event );
...
event.fire();
So, example above gives you class/method uniqueness, and if you need object/method uniqueness, then you should have an struct, which provides combined hash, assuming that there is std::hash<MyObject> and there is already std::hash<std::type_index> for a member function pointer.

Equality comparison of unions?

Is there a standard (or at least safe) way to compare unions for equality in C and/or C++? I expect that bitwise comparison would be useful in a number of scenarios regardless of the last-assigned member in each union; for instance, a particular bit-pattern could be reserved to mean "value is uninitialized", and it would be useful to be able to check if the union is uninitialized without needing to specify an "active" member.
An example in C++ (though I think the concept extends to C using non-member functions):
union MyData
{
public:
// Assume I'm compiling this on a platform where the size of `int`
// exceeds the size of `char*`, or that I'm using some combination
// if `#ifdef`s or similar to ensure that my numeric type is sufficiently
// large, or that I include an extra member that is known to be
// exactly the size of the larger member, just for the sake of
// comparison.
int int_member;
char* ptr_member;
bool isInitialized() const
{
return (*this != INVALID_VAL);
}
bool operator==(MyData const& rhs)
{
return / * ??? */;
}
private:
constexpr MyData INVALID_VAL { /* ??? */ };
}
// ... later, in client code...
MyData d;
bool initialized{d.isInitialized()}; // false
d.ptr_member = new char[32];
bool initialized{d.isInitialized()}; // true
Here, INVALID_VAL could probably be defined by setting int_member to the max negative int value, because that's an uneven value, so it won't be on a word boundary and therefore is highly unlikely to ever be assigned to the char* member (assuming that assignments typically come directly from new).
One possible implementation of operator== would be simply:
return int_member == rhs.int_member;
Even though it's not known whether int_member is the "active" member, I expect this to be safe, because I see no reason why a static cast from char* to int should fail or be problematic. Is that correct?
If this implementation is unsafe, something like the following should be possible (using C-style casts in C, of course):
return static_cast<void*>(*this) == static_cast<void*>(rhs);
...though of course if MyData is larger than the size of a pointer, you'd have to start messing around with sizeof to make this work.
Does anyone do this? Is the first (simpler) implementation safe? Is there any reason not to do it?
I think a better approach would be to wrap your union inside a class or struct with an enum field storing which was the last member accessed e.g.
class MyData {
enum {
unintialized, int_member, ptr_member
} last_member = unintialized;
union {
int int_member;
char* ptr_member;
} union_fields;
public:
bool isInitialized() const
{
return last_member != unintialized;
}
};
The in class initialization of last_member works if you have C++11 otherwise just initialize it in the default constructor.
Create accessors for the two fields and set last_member accordingly, it would also be good to add checks in the accessor methods making sure only the "active member" can be accessed.
Of course it's unsafe.
You can't assume that an int has the same size as a char *, for instance. There might also be padding, which is often random in content.

If I can take a normal pointer to a variable within a class, what's the point of class member pointers?

Take this code:
struct mystruct
{
int var;
mystruct() : var(0) {}
};
int main()
{
mystruct ins;
int* p = &ins.var;
*p = 1;
}
So what are some concrete really good examples of uses for the class member pointer?
int X::*p = &X::data; /* p contains offset */
X object;
X *objptr = new X;
int i = object.*p;
int j = objptr->*p;
It seems the your question is about pointers of pointer-to-data-member type. In C++ there are also pointers of pointer-to-member-function type. The two have something in common at some abstract level, but otherwise they are different.
Pointer of pointer-to-data-member type is applicable to any instance of the class. An ordinary pointer always points to a member of a specific instance of the class. Pointer of pointer-to-data-member type is a higher level implementation of the idea of "run-time offset" from the beginning of a class object. It is a relative pointer. In that way it is completely different thing from ordinary pointers, which are absolute pointers.
To illustrate that with an example, let's say you have an array of classes with three members of the same type x, y and z
struct Point { int x, y, z; };
Point point_array[N];
and you need to set all members with the same name to 0 in the entire array, without changing any other members. Here's how you can do it using a pointer of pointer-to-data-member type
void zero_members(Point points[], int n, int Point::*m) {
for (int i = 0; i < n; ++i)
points[i].*m = 0;
}
Now, by using this function you can do
zero_members(point_array, N, &Point::x);
to set all xs to zero. Or you can do
zero_members(point_array, N, &Point::y);
to set all ys to zero. You do can all this with a single function, and what's also important, the member selection is performed by a run-time parameter (as opposed to a compile-time one).
You cannot do something like this with ordinary pointers. In fact, you can't do it in any other way.
So that you can access the member on multiple instances.
Uhm, this is akin to asking for good examples of using goto... ;-) Okay, it's there, and sometimes it can be handy, but any example of "good" use of goto is likely to be disputed. And so also for member pointers, they're pretty low-level.
For member function pointers one common usage is in event-driven systems. Package a member function pointer together with a pointer to an object of the relevant class, and you have something very much like a C# delegate. You can then pass this little functor package around, and someone else's code, e.g. a GUI framework, can call back on your object without even knowing about it.
The Boost library provides some support for that, e.g. boost::function and boost::bind, and so does TR1 and the upcoming C++0x standard library (essentially a subset of the Boost functionality).
I can't offhand think of any common usage of data member pointers, though.
Regarding "low level", member pointers follow special rules which let you inadvertently bypass a protected access restriction.
Cheers & hth.,
– Alf

How to hash and compare a pointer-to-member-function?

How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function?
Example:
I have several bool (Class::*functionPointer)() (not static) that point to several diferent methods of the class Class and i need to hash those pointer-to-member-function.
How can i do that?
Also how can i compare (std::less) those member function pointers so i can store them in a std::set?
All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So you could try:
bool (Class::*fn_ptr)() = &Class::whatever;
const char *ptrptr = static_cast<const char*>(static_cast<const void*>(&fn_ptr));
Now treat ptrptr as pointing to an array of (sizeof(bool (Class::*)())) bytes, and hash or compare those bytes. You can use unsigned char instead of char if you prefer.
This guarantees no false positives - in C++03, pointers to member functions are POD, which means among other things that they can be copied using memcpy. This implies that if have the same byte-for-byte values, then they are the same.
The problem is that the storage representation of member function pointers could include bits which do not participate in the value - so they will not necessarily be the same for different pointers to the same member function. Or the compiler might, for some obscure reason, have more than one way of pointing to the same function of the same class, which are not byte-wise equal. Either way you can get false negatives. You'll have to look into how member function pointers actually work on your implementation. It must implement operator== for member function pointers somehow, and if you can find out how then you can probably figure out an order and a hash function.
That's potentially hard: member function pointers are awkward, and the storage is likely to include different amounts of non-participating "slack space" according to what kind of function is pointed to (virtual, inherited). So you'll probably have to interact quite significantly with your compiler's implementation details. This article might help get you started: http://www.codeproject.com/KB/cpp/FastDelegate.aspx
A cleaner alternative might be to do a linear search through an array in order to "canonicalise" all your function pointers, then compare and hash based on the position of the "canonical" instance of that function pointer in your array. Depends what your performance requirements are. And even if there are requirements, does the class (and its derived classes) have so many functions that the linear search will take that long?
typedef bool (Class::*func)();
vector<func> canon;
size_t getIndexOf(func fn_ptr) {
vector<func>::iterator it = find(canon.begin(), canon.end(), fn_ptr);
if (it != canon.end()) return it - canon.begin();
canon.push_back(func);
return canon.size() - 1;
}
I could not cast the pointer (in Microsoft compiler 2010)as described in previous answer but this works for me:
static string fmptostr(int atype::*opt)
{
char buf[sizeof(opt)];
memcpy(&buf,&opt,sizeof(opt));
return string(buf,sizeof(opt));
}
About bitwise identity of the pointer, it can be bitwise so it seems if appropriate compiler switches are used. At least this is true for Microsoft compiler E.g
using #pragma pointers_to_members
and a switch.../vmg
If your member function pointer is unique, which is true in most of cases for callback-based subscriptions, then you can use the tick with type_index, which uniqueness is guaranteed by uniqueness of type (i.e. Class::Method) in your program, and it is suitable to be stored in unordered_map, i.e.
struct MyEvent {
using fn_t = std::function<void(MyEvent &)>;
using map_t = std::unordered_map<std::type_index, fn_t>;
template <typename Handler>
void subscribe(Object& obj, Handler&& handler) {
fn_t fn = [&, handler = std::move(handler)](MyEvent& event) {
(obj.*handler)(event);
}
std::type_index index = typeid(Handler);
subscribers.emplace(std::move(index), std::move(fn));
}
void fire() {
for(auto& pair: subscribers) {
auto& fn = pair.second;
fn(*this);
}
}
map_t subscribers;
}
And the subscription and fire event example:
MyEvent event;
MyObject obj = ...;
event.subscribe(obj, &MyObject::on_event );
...
event.fire();
So, example above gives you class/method uniqueness, and if you need object/method uniqueness, then you should have an struct, which provides combined hash, assuming that there is std::hash<MyObject> and there is already std::hash<std::type_index> for a member function pointer.