2

I have a function that takes a pointer to a member function (along with the necessary arguments) and calls the function on a class instance. Is there a way to not have to pass the default arguments or somehow get their value? (Ignore the static instance)

class X
{
public:
    void func(int p = 10);
}

static X x;

template <typename... Args>
void call(void (X::* f)(Args...), Args... args)
{
    // do something

    (x.*f)(args...);

    // do something
}

void main()
{
    call(&X::func);      //doesn't compile
    call(&X::func, 10);  //have to know the default argument for this
}

I'm trying to replace this macro

#define CALL(...) \
{ \
    // some setup \

    __VA_ARGS__ \

    //some cleanup \
}

static X x;

void main()
{
    CALL(x.func();)
}
SMMB
  • 107
  • 6
  • 2
    No, the defaults are only part of the declaration, the pointer only contains the type which doesn't include the defaults – Alan Birtles Apr 05 '23 at 20:39
  • 1
    Default argument values are not part of a function's type. The compiler knows what they are, and can apply them as needed only at any site where the function is called *directly*. But all default info is lost if the function is called *indirectly*, as in this case. – Remy Lebeau Apr 05 '23 at 22:31

0 Answers0