小编RAl*_*len的帖子

标准重载的 std::abs 与 std::function<double (double)> 不匹配

我收到以下错误

min.cpp:17:30: error: no viable conversion from '<overloaded function type>' to 'Container::UnaryFun' (aka 'function<double (double)>')
    this->addFunction("abs", abs);

Run Code Online (Sandbox Code Playgroud)

当尝试编译以下代码时:

#include <cmath>
#include <string>
#include <functional>

class Test
{
public:
  using UnaryFun  = std::function<double (double)>;

  Test()
  {
    this->addFunction("abs", abs);
  }
  auto addFunction(const std::string& name, UnaryFun fun) -> void
  {
    // ...
  }
};

auto main() -> int {
  Test eval;
  return 0;
}

Run Code Online (Sandbox Code Playgroud)

我尝试检查std::absfor 参数double和返回类型的声明double,如下所示:

inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
  return __builtin_fabs(__lcpp_x);
}
Run Code Online (Sandbox Code Playgroud)

在 …

c++ macos g++ std

3
推荐指数
1
解决办法
143
查看次数

标签 统计

c++ ×1

g++ ×1

macos ×1

std ×1