【问题标题】:clang-6 and -std=c++17 -- can't invoke bind(2)clang-6 和 -std=c++17 -- 不能调用 bind(2)
【发布时间】:2018-01-21 01:48:08
【问题描述】:

尝试使用带有-std=c++17标志的clang++ 6.0版编译以下代码:

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

我收到以下错误:

.../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
....

不知何故,完美的标准函数 bind(2) 似乎被重新声明为返回 error_code 而不是旧的 int

发生了什么事?我该如何很好地解决这个问题——并保持代码可以与早期的编译器编译?

【问题讨论】:

  • ::bind - 并确保包含相关的头文件。
  • 好像有人用过using namespace std;
  • 确实,该行位于文件顶部。 std::bind 有什么问题,我将如何检查 its 结果?..
  • 它什么都没有,除了这不是你想在这里使用的那个(::bind,全局范围内的好那个)。但是using namespace std;(也)在作用域中引入了它。
  • 不,std::bind 与套接字无关。

标签: c++ network-programming c++17


【解决方案1】:

您的程序中可能有using namespace std 或类似名称,在这种情况下,编译器可能会认为您需要std::bind

要引用您可能真正想要的绑定,您可以使用::bind

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多