相关疑难解决方法(0)

如何指定指向重载函数的指针?

我想将重载函数传递给std::for_each()算法.例如,

class A {
    void f(char c);
    void f(int i);

    void scan(const std::string& s) {
        std::for_each(s.begin(), s.end(), f);
    }
};
Run Code Online (Sandbox Code Playgroud)

我期望编译器f()通过迭代器类型来解析.显然,它(GCC 4.1.2)没有这样做.那么,我该如何指定f()我想要的?

c++ stl

128
推荐指数
6
解决办法
3万
查看次数

标准重载的 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++ ×2

g++ ×1

macos ×1

std ×1

stl ×1