I tried to bind a function with std::bind, it kept saying that there is no overload for my set of arguments. It worked with boost::bind. Whats the diffrence between std and boost bind?
Im using:
Microsoft Visual Studio Ultimate 2012 Version 11.0.60315.01 Update 2
boost 1.53.0
func declaration and bindings:
void Messenger::receiveCallback(const boost::system::error_code& error, size_t bytes_transferred, char* message, int bufferSize, tcp::socket* socketPtr, void(*onMessageReceivedCallback)(char* message, string hostname, int port, int length));
std::bind(&Messenger::receiveCallback, this, std::placeholders::_1, std::placeholders::_2, message, bufferSize, socketPtr, onMessageReceivedCallback);//error
boost::bind(&Messenger::receiveCallback, this, _1, _2, message, bufferSize, socketPtr, onMessageReceivedCallback);
errors:
105 IntelliSense: no instance of overloaded function "std::bind" matches the argument list argument types are: (void (Messenger::*)(const boost::system::error_code &error, size_t bytes_transferred, char *message, int bufferSize, boost::asio::ip::tcp::socket *socketPtr, void (*onMessageReceivedCallback)(char *message, std::string hostname, int port, int length)), Messenger *, std::_Ph<1>, std::_Ph<2>, char *, int, boost::asio::ip::tcp::socket , void ()(char *message, std::string hostname, int port, int length)) c:\Users\gospo_000\Source\Repos\Messenger\Messenger\Messenger\Messenger.cpp 176 4 Messenger
lots of errors like this one:
Error 81 error C2780: 'enable_if::value,std::_BindRx(_thiscall _Farg0::* )(_V0_t,_V1_t,_V2_t,_V3_t,_V4_t),_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t>,_Vx0_t,_Vx1_t>>::type std::bind(Rx (_thiscall _Farg0::* const )(_V0_t,_V1_t,_V2_t,_V3_t,_V4_t),_Vx0_t &&,_Vx1_t &&)' : expects 3 arguments - 8 provided c:\users\gospo_000\source\repos\messenger\messenger\messenger\messenger.cpp 176 1 tester2
Messenger.h
#include <string>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include "Connection.h"
#include "Message.h"
#include <unordered_map>
using namespace std;
using namespace boost::asio;
using boost::asio::ip::tcp;
class Messenger
{
//.............................
void receiveCallback(const boost::system::error_code& error, size_t bytes_transferred, char* message, int bufferSize, tcp::socket* socketPtr, void(*onMessageReceivedCallback)(char* message, string hostname, int port, int length));
};
Messenger.cpp
void Messenger::receiveCallback(const boost::system::error_code& error, size_t bytes_transferred, char* message, int bufferSize, tcp::socket* socketPtr, void(*onMessageReceivedCallback)(char* message, string hostname, int port, int length))
{
if(error)
{
string connectionId = getConnectionId(socketPtr->remote_endpoint());
connections_.erase(connectionId);
cout<<error.message();
}
else
{
onMessageReceivedCallback(message, socketPtr->remote_endpoint().address().to_string(), socketPtr->remote_endpoint().port(), bytes_transferred);
socketPtr->async_receive(
boost::asio::buffer(message, bufferSize),
boost::bind(&Messenger::receiveCallback, this, _1, _2, message, bufferSize, socketPtr, onMessageReceivedCallback));
}
}
The difference is that std::bind
takes only 5 arguments by default. Set _VARIADIC_MAX macro to 10 in order to increase the maximum arg number in variadic templates.