简体   繁体   English

g ++ 4.8.2:无法从函数指针转换为std :: function <>

[英]g++ 4.8.2: could not convert from function pointer to std::function<>

I'm trying to write a class that takes, as a constructor parameter, a factory function that creates another instance of the same class on a transformed version of the input. 我正在尝试编写一个类,该类将构造函数作为工厂函数,该函数在输入的转换版本上创建同一类的另一个实例。 For a trivial example, a functor class that takes an int, prints it, and returns another functor that prints the input's successor. 对于一个简单的示例,仿函数类接受一个int,将其打印,然后返回另一个仿函数,该仿函数将打印输入的后继。

I'm getting errors of the form 我收到表格错误

error: could not convert 'foo' from 'Type(*)(int)' to 'std::function<Type(int)>'

The only seems to arise with factory functions passed into constructors. 唯一似乎是将工厂函数传递给构造函数。

Using a function pointer instead of a std::function<> works fine, but I was hoping that with C++11 I'd be able to avoid function pointers. 使用函数指针代替std::function<>可以很好地工作,但是我希望使用C ++ 11可以避免使用函数指针。

Here's an example: 这是一个例子:

#include <functional>
#include <iostream>

// std::function-returning-int param in ctor with default value
// This works fine

class PrInc; // "print and increment"

using Worker = std::function<int(int)>;

int foo(int i) {
    std::cout << i << std::endl;
    return i+1;
}

class PrInc {
  public:
    PrInc(int i, Worker fn = foo) : i_(i), fn_(fn) {}
    int operator()() { return fn_(i_); }
  private:
    int i_;
    Worker fn_;
};

// std::function-returning-PrInc2 param in ctor with default value
// This fails, at least on g++ 4.8.2 --std=c++11

class PrInc2;

using Factory = std::function<PrInc2(int)>;
// Use function ptrs (instead of std::function<>s) and it works fine
//typedef PrInc2 (*Factory)(int);

PrInc2 bar(int);

class PrInc2 {
  public:
    PrInc2(int i, Factory fn = bar) : i_(i), fn_(fn) {}
    PrInc2 operator()() { return fn_(i_); }
  private:
    int i_;
    Factory fn_;
};

PrInc2 bar(int i) {
    std::cout << i << std::endl;
    return PrInc2(i+1);
    // error: could not convert 'bar' from 'PrInc2 (*)(int) to 'Factory {aka std::function<PrInc2(int)>'
}

int main() {
    auto p1 = PrInc {1};
    auto p2 = PrInc{p1()};
    p2();

    auto p3 = PrInc2 {1};
    // error: could not convert 'bar' from 'PrInc2 (*)(int) to 'Factory {aka std::function<PrInc2(int)>'
    auto p4 = p3();
    p4();

    return 0;
}

What am I doing wrong? 我究竟做错了什么?

EDIT: thelink2012's suggestion to try PrInc2(int i, Factory fn = Factory(bar)) works fine on the "repro" above, but fails on the motivating example (GitHub link; relevant code is erdos.h:35-54 around ParseStep and ParseFunction). 编辑: thelink2012的尝试PrInc2(int i, Factory fn = Factory(bar))的建议在上面的“ repro”上运行良好,但是在激励示例 (GitHub链接)上失败了;相关代码是erdos.h:35-54,位于ParseStep周围和ParseFunction)。 Back to the drawing board for a better repro.... 返回绘图板以获得更好的再现。

The following part of your code, which is the only part that appears to be relevant to the compiler error you're asking about, compiles cleanly with gcc 4.9.2 using the -std=c++11 mode: 代码的以下部分(这似乎是与您要询问的编译器错误有关的唯一部分)使用-std=c++11模式在gcc 4.9.2中进行了干净地编译:

#include <functional>
#include <iostream>

using Worker = std::function<int(int)>;

int foo(int i) {
    std::cout << i << std::endl;
    return i+1;
}

class PrInc {
  public:
    PrInc(int i, Worker fn = foo) : i_(i), fn_(fn) {}
    int operator()() { return fn_(i_); }
  private:
    int i_;
    Worker fn_;
};

Most likely a bug in the older gcc you're using. 最有可能是您使用的旧版gcc中的错误。 Upgrade to a more recent version of gcc. 升级到最新版本的gcc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM