std::函数无法区分重载函数

std::function fails to distinguish overloaded functions

本文关键字:函数 重载 std 法区      更新时间:2023-10-16

我试图理解为什么std::function无法区分重载函数。

#include <functional>
void add(int,int){}
class A {};
void add (A, A){}
int main(){
        std::function <void(int, int)> func = add;
}

在上面显示的代码中,function<void(int, int)>只能匹配其中一个函数,但它失败了。为什么会这样?我知道我可以通过使用lambda或指向实际函数的函数指针来解决这个问题,然后将函数指针存储在函数中。但为什么会失败呢?我想被选为哪一个职位的背景不清楚吗?请帮助我理解为什么失败,因为我无法理解为什么在这种情况下模板匹配失败。

我在clang上得到的编译器错误如下:

test.cpp:10:33: error: no viable conversion from '<overloaded function type>' to
      'std::function<void (int, int)>'
        std::function <void(int, int)> func = add;
                                       ^      ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_03:1266:31: note: 
      candidate constructor not viable: no overload of 'add' matching
      'std::__1::nullptr_t' for 1st argument
    _LIBCPP_INLINE_VISIBILITY function(nullptr_t) : __f_(0) {}
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_03:1267:5: note: 
      candidate constructor not viable: no overload of 'add' matching 'const
      std::__1::function<void (int, int)> &' for 1st argument
    function(const function&);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_03:1269:7: note: 
      candidate template ignored: couldn't infer template argument '_Fp'
      function(_Fp,
      ^
1 error generated.

编辑-除了微软的回答,我在这个论坛上做了一些搜索,找到了失败的确切原因。我从纳瓦兹在这篇帖子中的回复中得到了答案。

我把他的答案粘贴在这里:

    int test(const std::string&) {
        return 0;
    }
    int test(const std::string*) {
        return 0;
    }
    typedef int (*funtype)(const std::string&);
    funtype fun = test; //no cast required now!
    std::function<int(const std::string&)> func = fun; //no cast!

那么,为什么std::function<int(const std::string&)>不能像funtype fun = test那样工作呢?

答案是,因为std::function可以用任何对象初始化,因为它的构造函数是模板化的,这与传递给std::function的模板参数无关

我们很清楚你打算选择哪个函数,但编译器必须遵循C++的规则,不要使用聪明的逻辑跳跃(甚至不像在这样的简单情况下那样聪明!(

std::function的相关构造函数为:

template<class F> function(F f);

它是一个接受任何类型的模板。

C++14标准确实限制了模板(由于LWG DR 2132(,使其:

除非参数类型ArgTypes...和返回类型Rf是可调用的(20.9.12.2(,否则不应参与重载解析。

这意味着编译器将只允许在Functorstd::function的调用签名(在您的示例中是void(int, int)(兼容时调用构造函数。理论上,这应该意味着void add(A, A)不是一个可行的论点,所以"很明显"你打算使用void add(int, int)

但是,编译器在知道f的类型之前无法测试"f对参数类型是可调用的…"约束,这意味着它需要在void add(int, int)void add(A, A)之间消除歧义,然后才能应用允许它拒绝其中一个函数的约束!

因此,这是一种先有鸡后有蛋的情况,不幸的是,这意味着您需要通过指定要使用的add的确切重载来帮助编译器,然后编译器可以应用约束,并(相当冗余地(决定它是构造函数可接受的参数。

可以想象,我们可以更改C++,这样在类似的情况下,所有重载函数都会根据约束进行测试(所以在测试之前我们不需要知道要测试哪一个(,如果只有一个可行,那么就使用那个,但C++不是这样工作的。

虽然很明显您想要什么,但问题是std::function无法影响&add的过载解决方案。如果要初始化一个原始函数指针(void (*func)(int,int) = &add(,它确实可以工作。这是因为函数指针初始化是在其中进行重载解析的上下文。目标类型是完全已知的。但是std::function将接受几乎所有可调用的参数。在接受参数方面的灵活性意味着您不能在&add上进行过载解析。CCD_ 26的多个过载可能是合适的。

显式演员阵容将起作用,即static_cast<void(*)(int, int)> (&add)

这可以封装在template<typename F> std::function<F> make_function(F*)中,这将允许您编写auto func = make_function<int(int,int)> (&add)

尝试:

std::function <void(int, int)> func = static_cast<void(*)(int, int)> (add);

void add(A, A)void add(int, int)的地址明显不同。当您按名称指向函数时,编译器几乎不可能知道您需要哪个函数地址。这里的void(int, int)不是提示。

处理此问题的另一种方法是使用C++14中的泛型lambda:

int main() {
    std::function<void(int, int)> func = [](auto &&... args) {
        add(std::forward<decltype(args)>(args)...);
    };
}
    

这将创建一个lambda函数,该函数将毫无歧义地解决问题。我没有提出论点,

据我所知,这是一个Visual Studio问题。

c++11标准(20.8.11(

std::function synopsis
template<class R, class... ArgTypes> class function<R(ArgTypes...)>;

但是VisualStudio没有那种专门化的

clang++和g++完全可以重载std::函数

之前的回答解释了为什么VS不起作用,但他们没有提到这是VS的错误