1

Trying to compile the following code using clang++ version 6.0 with the -std=c++17 flag:

   if (bind(ssock, res->ai_addr, res->ai_addrlen) != 0)
   {
      return -1;
   }

I get the following error:

.../udt4/app/test.cpp:90:51: error: invalid operands
      to binary expression ('__bind<int &, sockaddr *&, unsigned int &>' and
      'int')
   if (bind(ssock, res->ai_addr, res->ai_addrlen) != 0)
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~
/usr/include/c++/v1/system_error:587:1: note: candidate function not viable: no
      known conversion from '__bind<int &, sockaddr *&, unsigned int &>' to
      'const std::__1::error_code' for 1st argument
operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
....

Somehow, the perfectly standard function bind(2) seems to have gotten redeclared as something returning an error_code instead of the good old int.

What's going on? How do I solve this nicely -- and keep the code compilable with earlier compilers?

Mikhail T.
  • 3,043
  • 3
  • 29
  • 46
  • 1
    `::bind` - and make sure you include the relevant header file. –  Jan 21 '18 at 01:53
  • 1
    Seems like somebody used [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?s=1|816.8507). – O'Neil Jan 21 '18 at 01:54
  • Indeed, the line is found at the top of the file. What's wrong with `std::bind` and how would I check _its_ result?.. – Mikhail T. Jan 21 '18 at 02:09
  • 1
    There is nothing with with it, except this is not the one you want to use here ([`::bind`](https://linux.die.net/man/2/bind), the good one in the global scope). But `using namespace std;` (also) introduces it in the scope. – O'Neil Jan 21 '18 at 02:12
  • But, I guess, the intent was for the `std::bind` to be the drop-in replacement for the `::bind` -- and it is, probably, "more C++ish". How is one supposed to check its result, however? – Mikhail T. Jan 21 '18 at 02:13
  • 1
    No, [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind) has nothing to do with sockets. – O'Neil Jan 21 '18 at 02:14

1 Answers1

2

You might have using namespace std or similar in your program, in which case the compiler might think you want std::bind.

To refer to the bind you probably really want, you can use ::bind.

Ove
  • 770
  • 4
  • 9