3
template<typename T, typename M, M Method>
class ProxyObject
{
public:

    template<typename... Args>
    void Invoke (T& Object, _In_ Args&&... A)
    {
        (void)(Object.*Method)(std::forward<Args>(A)...);
    }
};

class Object
{
public:

    int MyMethod (int Val)
    {
        wcout << L"Hello!" << endl;
        return Val;
    }
};


int wmain ()
{
    Object myObj;
    ProxyObject<Object, decltype(&Object::MyMethod), &Object::MyMethod> obj;

    obj.Invoke(myObj, 10);

    return 0;
}

The decltype(&Object::MyMethod) seems redundant in the definition of obj. Is there any way to make the ProxyObject automatically infer the type of the pointer-to-member-function being passed, so that I can define obj as follows:

ProxyObject<Object, &Object::MyMethod> obj;
2
  • 2
    P0127R0 Commented Oct 10, 2015 at 2:56
  • @cpplearner That's nice. The timing is almost uncanny. Still hoping for a solution that I can use today though 😊 Commented Oct 10, 2015 at 3:01

1 Answer 1

2

I think it's impossible for class template, because you have to specify the member function type explicitly.

Template function could help you lot with the argument deduction:

template<typename T, typename M, typename... Args>
void invoke (T& Object, M Method, Args&&... A)
{
    (void)(Object.*Method)(std::forward<Args>(A)...);
}

then

invoke(myObj, &Object::MyMethod, 10);

LIVE

Sign up to request clarification or add additional context in comments.

2 Comments

That's unfortunate. I do want the member function to be part of the class type though. Thanks for the suggestion.
Class template need specify the template parameter explicity, and there're both type template parameter and nontype template parameter, so...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.