0

Currently, I'm trying to check if the bind() statement fails in my code. From what I know, the way to go about this would be to check this condition:

if (bind(sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) < 0) 
{
   G4cerr <<  "bind failed: " << strerror(errno) << " " << __FILE__ << ":" << __LINE__ << G4endl;
   return(-1);
}

But I get the following error when I do this:

error: invalid operands to binary expression ('__bind<int &, sockaddr *, unsigned long>' and 'int')

I can see that '<' is a binary operation, which cannot compare two different data type but can't understand the reason for bind() not returning an integer value.

What am I missing here?

PS. This is a part of a GEANT4 application that someone in my lab wrote.

  • 3
    You have accidentally run into a naming collision with [std::bind](http://en.cppreference.com/w/cpp/utility/functional/bind) – user4581301 Jan 23 '17 at 23:50
  • 2
    Use ::bind to select the socket function rather than the one in the Standard Library. –  Jan 23 '17 at 23:52
  • @user4581301 I thought I was using the std::bind without explicitly mentioning it. Can you elaborate? – Shobhit Sharma Jan 23 '17 at 23:55
  • @NeilButterworth Can you elaborate? – Shobhit Sharma Jan 23 '17 at 23:56
  • @Shob There is a bind function in the standard library, in the std namespace, which has nothing to do with sockets. Using ::bind selects a function in the global namespace, which is where the socket bind function lives. –  Jan 23 '17 at 23:58
  • note the `<` and `>` in the error message `__bind`. The compiler thinks you are trying to instantiate a `std::bind` object rather than call the `bind` function. This is probably because of a `using namespace std` in the file or a header pulling `std::bind` into the global namespace where it has the same name as the function `bind`. – user4581301 Jan 23 '17 at 23:58
  • @NeilButterworth Damn! Thanks. That worked. :) – Shobhit Sharma Jan 24 '17 at 00:00
  • @user4581301You got it. Thanks a lot! :) – Shobhit Sharma Jan 24 '17 at 00:01

0 Answers0