0

Hi I am having an issue compiling the following code. I am using auto and std::bind to bind a callback function with arguments. However, after passing this callback function as a parameter, it has issues compiling. Do you see an issue with the function declarations below:

#include <iostream>
#include <functional>
using namespace std;

class VmapPlayer
{
    public:
        void startPlayback();
        void playAdBreak(int adBreak, void (VmapPlayer::*callback)());
        void playSingleAd(int ad, void (VmapPlayer::*callback)(int adBreak, void (VmapPlayer::*cb)()));
};

void VmapPlayer::playSingleAd(int ad, void (VmapPlayer::*callback)(int adBreak, void (VmapPlayer::*cb)()))
{
    cout << "i am here" << endl;

    // OPTION #1 I would like to call this function
    //(this->*callback)(adBreak, cb);

    // OPTION #2 I would like this call this function without the params:
    //(this->*callback)();
}

void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)())
{
    auto cb = std::bind(&VmapPlayer::playAdBreak, adBreak, callback);
    playSingleAd(123, cb);
}

void VmapPlayer::startPlayback()
{
    playAdBreak(456, &VmapPlayer::startPlayback);
}

int main()
{
    VmapPlayer p;
    p.startPlayback();

    return 0;
}

Please see below for the compile error log:

main.cpp||In member function 'void VmapPlayer::playAdBreak(int, void (VmapPlayer::*)())':|
main.cpp|28|error: no matching function for call to 'VmapPlayer::playSingleAd(int, std::_Bind<std::_Mem_fn<void (VmapPlayer::*)(int, void (VmapPlayer::*)())>(int, void (VmapPlayer::*)())>&)'|
main.cpp|28|note: candidate is:|
main.cpp|14|note: void VmapPlayer::playSingleAd(int, void (VmapPlayer::*)(int, void (VmapPlayer::*)()))|
main.cpp|14|note:   no known conversion for argument 2 from 'std::_Bind<std::_Mem_fn<void (VmapPlayer::*)(int, void (VmapPlayer::*)())>(int, void (VmapPlayer::*)())>' to 'void (VmapPlayer::*)(int, void (VmapPlayer::*)())'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|

I supposed my question can be simplified as:

What does the function declaration for playSingleAd() need to be in order to compile the following successfully?:

void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)())
{
    auto cb = std::bind(&VmapPlayer::playAdBreak, adBreak, callback);
    playSingleAd(123, cb);
}
code
  • 5,294
  • 16
  • 62
  • 113

1 Answers1

1

When you bind a method to fully-supplied parameters, the resulting functor takes no arguments. In your code, playSingleAd takes a function pointer that takes as an argument, a function pointer whose arguments are to be supplied when called. Because you already bound the arguments to that function pointer, the arguments thereto cannot not be specified in the function signature.

Anyway, your code can be improved with the use of std::function. Moreover, the function will have to take the instance as an argument, as shown in the implementation of playSingleAd below:

class VmapPlayer
{
public:
    void startPlayback();
    void playAdBreak(int adBreak, void (VmapPlayer::*callback)());
    void playSingleAd(int ad, std::function<void (VmapPlayer&)>);
};

void VmapPlayer::playSingleAd(int, std::function<void (VmapPlayer&)> callback)
{
    cout << "i am here" << endl;
    callback(*this);
}

void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)())
{
    using namespace std::placeholders;
    auto cb = std::bind(&VmapPlayer::playAdBreak, _1, adBreak, callback);
    playSingleAd(123, cb);
}
David G
  • 94,763
  • 41
  • 167
  • 253
  • Thanks for the reply. I did not know that! Ahh, I updated my code but it still seems to have trouble compiling. Do you see anything wrong with my function declarations? – code Jan 22 '14 at 21:05
  • It states the problem is on line "playSingleAd(123, cb);" - error: no matching function for call to 'VmapPlayer::playSingleAd()...." – code Jan 22 '14 at 21:08
  • Is this because "cb" has data type "auto" ? And the function declaration of playSingleAd() has issues with that? – code Jan 22 '14 at 21:09
  • @user1456962 Is that the entire error message? I just want to make sure that I see everything you're seeing. Also, can you copy-paste the errors to your question? – David G Jan 22 '14 at 21:10
  • I copy pasted the errors to the bottom of the question. Thanks – code Jan 22 '14 at 21:16
  • @user1456962 Why are you binding the member function from inside the actual member function?? `std::bind(&VmapPlayer::playAdBreak, ...)` – David G Jan 22 '14 at 21:25
  • This is a snippet of the application but the idea is to pass the callback for playAdBreak (along with the 2 parameters - adBreak/callback) to be passed to playSingleAd() – code Jan 22 '14 at 21:31
  • Hi 499602D2, I added more information at the bottom of the post. I suppose my question is "What does the function declaration for playSingleAd() need to be in order to accept an argument of type "auto"? – code Jan 22 '14 at 21:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45841/discussion-between-user1456962-and-0x499602d2) – code Jan 22 '14 at 21:44