【问题标题】:Passing function into function with std::function and template argument使用 std::function 和模板参数将函数传递给函数
【发布时间】:2020-12-17 06:27:10
【问题描述】:

我正在尝试将指向谓词函数的指针传递给FooBar 函数。 Bar 函数可以正常工作,但 Foo 函数会引发编译时错误:

错误:没有匹配函数调用Foo<int>(bool (&)(int))

为什么编译器会报错? FooBar的模板参数类型在Args'解包后有什么区别吗?

#include <functional>

bool predicate(int a) {
    return (a > 5);
}

// sizeof...(Args) == 1 and I suppose it is int
template<typename... Args>
void Foo(std::function<bool(Args...)> predicate) {
    // clang: note: candidate template ignored:
    //        could not match 'function<bool (int, type-parameter-0-0...)>' 
    //        against 'bool (*)(int)'
}

template<typename Args>
void Bar(std::function<bool(Args)> predicate) {

}

int main(int argc, char const *argv[]) {
    // gcc: error: no matching function for call to
    //      'Foo<int>(bool (&)(int))'
    Foo<int>(predicate);
    Bar<int>(predicate);
    
    return 0;
}

See Compiler Explorer for a live example.

我还尝试稍微更改 Foo 函数,它以某种方式工作:

template<typename... Args>
void Foo(bool(*predicate)(Args...)) {
  std::function<bool(Args...)> func(predicate);
}

我想在Foo 函数中有std::function 类型参数,但我不知道该怎么做

【问题讨论】:

    标签: c++ templates variadic-templates type-inference std-function


    【解决方案1】:

    错误是因为std::function 的确切类型与predicate 不同。为了解决这个问题,你可以显式调用std::function的构造函数:

    int main() {
        Foo<int>( std::function<bool(int){predicate} );
        //OR
        Foo<int>( {predicate} );
        return 0;
    }
    

    【讨论】:

    • 我发现你必须调用构造函数感到困惑。 std::function 可以从函数指针隐式构造。知道为什么这里不可能进行这种隐式转换吗?
    • @J.Schultke 不完全确定,它适用于普通的 std::function&lt;bool(...)&gt; 变量,但如果它是函数参数则不起作用。有一个类似的问题:stackoverflow.com/questions/9242234/… 但遗憾的是没有明确的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    相关资源
    最近更新 更多