I am writing a test fixture which involves ensuring certain callbacks are called at appropriate times (actually Qt signals, but it shouldn't matter for the sake of my problem). To help with this, I created a helper class that records when a callback (signal) fires into a list.
This list needs to be able to record which callback (signal) fired. I would also prefer to not need to create a new enumeration specifically for this purpose. My idea was to instead record the address of the signal as a type-erased pointer so I can check the record against the address of the signal.
To make things a little easier on myself, I record the signal type as a:
template <typename Object>
class SignalType
{
public:
SignalType() = default;
SignalType(SignalType const&) = default;
SignalType(SignalType&&) = default;
template <typename R, typename... Args>
SignalType(R (Object::*member)(Args...))
: member{reinterpret_cast<void (Object::*)()>(member)} {}
template <typename R, typename... Args>
bool operator==(R (Object::*other)(Args...)) const
{ return this->member == reinterpret_cast<void (Object::*)()>(other); }
private:
void (Object::*member)() = nullptr;
};
This "hides" the type erasure from the point of use, so I can later just write:
QCOMPARE(event.type, &SomeObject::someMethod);
...without needing to clutter that with a cast.
However, GCC is unhappy:
warning: cast between incompatible pointer to member types from ‘void (SomeObject::*)(...)’ to ‘void (SomeObject::*)()’ [-Wcast-function-type]
Is there a way to make GCC happy without resorting to diagnostic #pragmas to simply shut up the warning? Is there some other, "better" way to achieve this particular flavor of type-erasure? (Note that I don't need to ever call the member that SignalType encapsulates; I just need to be able to test for equality.)
Sigh. Should search on the warning message, not what I'm trying to do. Technically I guess this is a duplicate of Cast Between Incompatible Function Types in gcc, however that only asks how to get rid of the warning, and isn't clear what the code is trying to accomplish. So, in order that I might learn something useful here, please focus on if there is some other, "cleaner" way to accomplish my goal rather than just closing this as a duplicate and saying "it can't be fixed".