std::函数无法区分重载函数
std::function fails to distinguish overloaded functions
我试图理解为什么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...
和返回类型R
的f
是可调用的(20.9.12.2(,否则不应参与重载解析。
这意味着编译器将只允许在Functor
与std::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的错误
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- c++:可变模板和函数重载
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- 解决模板成员函数重载
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 推断模板化函数中的函数重载
- C++复制函数重载导致"must be a nonstatic member function"错误
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- 可变参数泛型 lambda 和函数重载
- C++中的函数重载和继承
- 当有右值构造函数可用时,为什么从右值调用类引用构造函数重载?
- C/C++ 可变参数宏函数重载
- 将基类的成员函数重载到其他派生类C++
- C++ 函数重载匹配
- C++函数重载,具体步骤是什么
- C++:使用 param pack 显式调用函数重载
- 隐式生成的函数重载用于右值参数?
- 使用函数重载输入运算符
- 运算符重载函数上的函数重载