0
votes

I'm having issues with getting a partially-qualified function object to call later, with variable arguments, in another thread.

In GCC, I've been using a macro and typedef I made but I'm finishing up my project an trying to clear up warnings.

#define Function_Cast(func_ref) (SubscriptionFunction*) func_ref
typedef void(SubscriptionFunction(void*, std::shared_ptr<void>));

Using the Function_Cast macro like below results in "warning: casting between pointer-to-function and pointer-to-object is conditionally-supported"

Subscriber* init_subscriber = new Subscriber(this, Function_Cast(&BaseLoaderStaticInit::init), false);

All I really need is a pointer that I can make a std::bind<function_type> object of. How is this usually done?

Also, this conditionally-supported thing is really annoying. I know that on x86 my code will work fine and I'm aware of the limitations of relying on that sizeof(void*) == sizeof(this*) for all this*.

Also, is there a way to make clang treat function pointers like data pointers so that my code will compile? I'm interested to see how bad it fails (if it does).

Relevant Code:

#define Function_Cast(func_ref) (SubscriptionFunction*) func_ref
typedef void(SubscriptionFunction(void*, std::shared_ptr<void>));
typedef void(CallTypeFunction(std::shared_ptr<void>));

Subscriber(void* owner, SubscriptionFunction* func, bool serialized = true) {
    this->_owner = owner;
    this->_serialized = serialized;
    this->method = func;

    call = std::bind(&Subscriber::_std_call, this, std::placeholders::_1);
}

void _std_call(std::shared_ptr<void> arg) { method(_owner, arg); }
2
Non-static member functions can not be used as ordinary pointers to (non-member) functions. You have C++11, why not use std::function and std::bind? - Some programmer dude
@JoachimPileborg I tried to use std::function first, mostly because of the name, but I had problems with converting a std::function<void(Type1::this*, shared_ptr)> to a std::function<void(void*, shared_ptr)> so that I could store a lot of them in the same structure. Answering this conversion would also be an answer to the question. - Bryan Koch
@lorro I haven't tried lambdas simply because they appear to have to be constructed with known arguments in the capture clause whereas I need to pass an unknown shared_ptr<void>. Also, can I store different lambdas in the same vector-like structure? - Bryan Koch
Just an observation, but needing pointers to void is usually a sign of design problems. Type-safety is one of the good and nice things about C++. Actively trying to go around it will in the end mean worse code that is harder to maintain and get to grips with for new readers of the code (and with "new readers" I include yourself in a few months when you're starting to forget the details). - Some programmer dude
@JoachimPileborg Thanks for the concern but (as far as I know) this is one of the few cases where storage has to be done as void*'s. I've written a typedef above which allows the compiler to enforce the function declaration. If you have an alternative to storing function objects from different classes in the same container then I'd be glad to change over to it. Also, I'm not a "new reader." This question stems from a snippet of a project akin to Intel TBB but almost fully automatic and is the main code behind a future submission to the SC conference. - Bryan Koch

2 Answers

0
votes

The problem here is that you are trying to use a member-function pointer in place of a function pointer, because you know that, under-the-hood, it is often implemented as function(this, ...).

struct S {
    void f() {}
};

using fn_ptr = void(*)(S*);

void call(S* s, fn_ptr fn)
{
    fn(s);
    delete s;
}

int main() {
    call(new S, (fn_ptr)&S::f);
}

http://ideone.com/fork/LJiohQ

But there's no guarantee this will actually work and obvious cases (virtual functions) where it probably won't.

Member functions are intended to be passed like this:

void call(S* s, void (S::*fn)())

and invoked like this:

(s->*fn)();

http://ideone.com/bJU5lx

How people work around this when they want to support different types is to use a trampoline, which is a non-member function. You can do this with either a static [member] function or a lambda:

auto sub = new Subscriber(this, [](auto* s){ s->init(); });

or if you'd like type safety at your call site, a templated constructor:

template<typename T>
Subscriber(T* t, void(T::*fn)(), bool x);

http://ideone.com/lECOp6

If your Subscriber constructor takes a std::function<void(void))> rather than a function pointer you can pass a capturing lambda and eliminate the need to take a void*:

new Subscriber([this](){ init(); }, false);
0
votes

it's normally done something like this:

#include <functional>
#include <memory>

struct subscription
{
  // RAII unsubscribe stuff in destructor here....
};

struct subscribable
{
  subscription subscribe(std::function<void()> closure, std::weak_ptr<void> sentinel)
  {
    // perform the subscription

    return subscription {
      // some id so you can unsubscribe;
    };
  }


  //
  //

  void notify_subscriber(std::function<void()> const& closure, 
                         std::weak_ptr<void> const & sentinel)
  {
    if (auto locked = sentinel.lock())
    {
      closure();
    }
  }
};