Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish between bind() in sys/sockets.h and std::bind?

Tags:

c++

g++

I'm using g++ 4.6.0 to compile some C++ code that successfully compiled in earlier versions.

if ( bind(iControl, (struct sockaddr *) &sa, sizeof(sa)) == -1)
  throw runtime_error ("bind");

where iControl is a socket, and sa is a struct sockaddr_in.

However, in g++ 4.6 I get the following error:

comms.cpp:93:66: error: no match for ‘operator==’ in ‘std::bind(_Functor&&, _ArgTypes&& ...) [with _Functor = int&, _ArgTypes = {sockaddr*, long unsigned int}, typename std::_Bind_helper<_Functor, _ArgTypes>::type = std::_Bind<int(sockaddr*, long unsigned int)>]((* &((sockaddr*)(& sa))), (* &16ul)) == -0x00000000000000001’

comms.cpp:93:66: note: candidates are:

followed by about a page and a half of possible candidates.

It appears to be mixing up the bind function in sys/sockets.h with std::bind in functional . How do I disambiguate the two without rewriting my whole source file to remove using namespace std?

like image 332
Not Joe Bloggs Avatar asked Jul 01 '11 17:07

Not Joe Bloggs


1 Answers

Qualify it to be global: ::bind(...) (and make sure you have all the right headers included).

EDIT: (I got the idea from @Bo Persson's comment) Another solid option is to change using namespace std; to several using <thing> like:

using std::cout;
using std::endl;
using std::string;
// etc.

This lets your old code compile and doesn't bring std::bind into the global namespace.

like image 113
Mark B Avatar answered Sep 18 '22 16:09

Mark B