Callback class to callback member (virtual) functions After looking at a post on CR I found that pointers to member functions are incredibly hard to use, especially as callbacks. The current solution doesn't solve the problem mentioned in the linked post, but tries to get rid of the type erasure (i.e. std::function&lt;&gt;) by making the target type explicit. I tried to solve lifetime/ownership issue with something else than std::shared_ptr&lt;&gt;, but found that it is impossible without relying on programmer to take care of that. So, I ended up with std::shared_ptr&lt;&gt; as a solution.

So, here is the code:

#pragma once
#include &lt;memory&gt;
#include &lt;utility&gt;

template &lt;typename T, typename Func&gt;
class member_callback;

template &lt;typename T, typename R, typename ... ArgTypes&gt;
class member_callback&lt;T, R(ArgTypes...)&gt;
{
    std::shared_ptr&lt;T&gt; object;
    R(T::* callback)(ArgTypes ...);
public:
    using function_pointer = R(T::*)(ArgTypes ...);

    member_callback(const std::shared_ptr&lt;T&gt;&amp; ptr, R(T::* function)(ArgTypes...)) :
        object(ptr),
        callback(function)
    {}

    member_callback(std::shared_ptr&lt;T&gt;&amp;&amp; ptr, R(T::* function)(ArgTypes...)) :
        object(std::move(ptr)),
        callback(function)
    {}

    R operator()(ArgTypes&amp;&amp; ... args)
    {
        return ((*object).*callback)(std::forward&lt;ArgTypes&gt;(args)...);
    }

    std::shared_ptr&lt;T&gt; get()
    {
        return object;
    }

    function_pointer get_function()
    {
        return callback;
    }
};


It does one tradeoff though, you can't make a vector of them and call member functions of completely unrelated classes. Nevertheless, you'll still be able to invoke virtual functions, and they will work as expected according to this answer.

From the first glance it might look useless. But it is not! It becomes extremely useful when you're making a menu (1. ... 2. ... 3. ...) and want to invoke a function on the single object. So, here is the example:

#include "member_callback.hpp"
#include &lt;iostream&gt;
#include &lt;vector&gt;

class integer
{
    int x;
public:
    integer():
            x(0)
    {}

    integer&amp; operator++()
    {
        ++x;
        return *this;
    }

    integer&amp; operator--()
    {
        --x;
        return *this;
    }

    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const integer&amp; i);
};

std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const integer&amp; i)
{
    os &lt;&lt; "Currently number is: " &lt;&lt; i.x &lt;&lt; '\n';
}

void print_menu()
{
    static auto message = "Type the specified number to invoke the command.\n";
    static auto menu = "0. Increment\n1. Decrement\nAnything else to exit\n";

    std::cout &lt;&lt; message &lt;&lt; menu;
}

int main()
{
    auto object = std::make_shared&lt;integer&gt;();

    std::vector&lt;member_callback&lt;integer, integer&amp;()&gt;&gt; actions{{object, &amp;integer::operator++},
                                                              {object, &amp;integer::operator--}};

    std::cout &lt;&lt; "Welcome to dummy program!\n";
    std::cout &lt;&lt; *object;

    unsigned int response =  -1;
    print_menu();
    std::cin &gt;&gt; response;
    while (response &lt; 2)
    {
        actions[response]();
        std::cout &lt;&lt; *object;
        print_menu();
        std::cin &gt;&gt; response;
    }

    std::cout &lt;&lt; "Goodbye!\n";
}


If code will ever get changed you can find it here.

I would like a review to focus on the easy of use. Also, does the code provide fine grained control? It will require dealing with std::shared_ptr&lt;&gt;, but I think it is mostly ok. Any other review points are welcome!
Overall, the design looks reasonable.  However, I can't shake the feeling that Lambda expressions are a good (maybe even better) fit here.

For example, instead of

member_callback(object, &amp;integer::--);


you could use a Lambda expression:

[&amp;] () { return object--; }


This is almost as short, and has some additional advantages:


You aren't effectively "reinventing the wheel".  Although I wouldn't say your member_callback class is precisely the same as a lambda, it's pretty close.
I'm not qualified to say whether your code conforms to the C++ 11 specification.  But assuming it does, you are relying on relatively obscure portions of the specification.  This has some practical problems:


Others might have a hard time understanding and verifying your code.
Compilers often don't conform as strictly to the spec as we would like.  So you might find that your code is not portable, even if it does conform the the spec.

You noted that one of the drawbacks of your approach is that you cannot mix functions to different types of objects.  With lambdas, you can combine functions that work on different types of objects.  You aren't just limited to objects of a single (base) type (in your case, integer).  Likewise, you can define functions that operate on more than one object, or even none at all (e.g., produce a constant value).  Or, you could combine functions.
Lambdas open the door to higher-order approaches.  For example, if you Curry the member_callback function, you can partially instantiate a callback:


With the order you have (object, function), you can construct a factory for working on a single object.  For example (eliding types for brevity):

factory = member_callback_curried(object);

// Sometime later, perhaps in a different scope
dec = factory([] (integer&amp; obj) { return obj--; });
inc = factory([] (integer&amp; obj) { return obj++; });

Reversing the order to (function, object), you can construct a factory for working with the specified function:

factory = callback_member_curried([&amp;] (integer&amp; obj) { return obj-- });

// Sometime later, perhaps in a different scope
dec_o1 = factory(obj1); // function that decrements obj1
dec_o2 = factory(obj2); // function that decrements obj2