我有兩個函數foo
的重載,它們取不同的std::function
s,當與std::bind
的結果一起使用時,會導致後者的模糊問題。我不明白爲什麼只有這是不明確的。與std :: function參數不同的重載與bind(有時)是不明確的
void foo(std::function<void(int)>) {}
void foo(std::function<int()>) {}
void take_int(int) { }
int ret_int() { return 0; }
當使用int()
具有bind
函數I得到一個歧義錯誤
foo(std::bind(ret_int)); // ERROR
隨着GCC-5.1誤差(與鐺類似)
error: call to 'foo' is ambiguous
foo(std::bind(ret_int));
^~~
note: candidate function
void foo(std::function<void(int)>) {}
^
note: candidate function
void foo(std::function<int()>) {}
然而所有以下的工作
foo(std::bind(take_int, _1));
foo(take_int);
foo(ret_int);
foo([](){ return ret_int(); });
struct TakeInt {
void operator()(int) const { }
};
struct RetInt {
int operator()() const { return 0; }
};
foo(TakeInt{});
foo(RetInt{});
望着std::function
構造
template< class F >
function(F f);
它將使意義,我認爲對不同類型的std::function
多個重載任何函數應該有歧義,但它僅與綁定的電話的問題。然後我想「也許有一些神奇的事情處理函數類型和lambda表達式,它不處理實際的類,」但它也處理這些。
有上en.cppreference一張紙條,上面寫着[因爲C++ 14]
此構造函數不參與重載解析,除非f是可贖回的參數類型參數數量...並返回類型R
相關:http://stackoverflow.com/questions/30227097/c11-lambda-can-be-assigned-to-stdfunction-with-incorrect-signature和http://stackoverflow.com/questions/22146749/overload -resolution-with-stdfunction/22147471#22147471 – 0x499602D2