lundi 1 mai 2017

std::bind on member with call operator

This might be a silly and stupid thing to do - however I would like to understand what happens here.

I have the following code:

#include <iostream>
#include <functional>

using namespace std;

namespace
{
    struct call
    {
        void operator()() const
        {
            std::cout << "call::operator()" << std::endl;
        }
    };

    struct dummy
    {
        dummy() = default;
        dummy(const dummy&) = delete;

        call member;
    }; 
}

So member essentially would work like any other object method, allowing it to be invoked as:

dummy d;
d.member()

Which would print call::operator().

Now I would like to use bind to do that, the initial implementation looked like this:

int main() 
{
    dummy d;

    auto b = std::bind(&dummy::member, &d);
    b();
    return 0;
}

This compiles, but nothing is printed. We don't really understand what is happening - the fact that it compiles, but produces no output puzzles us :) surely some magic is going on inside the belly of std::bind, but what?

Here is a link to play with the code: http://ift.tt/2quPSB0

Aucun commentaire:

Enregistrer un commentaire