我在我的代碼的各個地方使用std::bind
遇到了很多麻煩。有時它有效,有時它不會,所以我認爲我正在做一些根本性錯誤的事情。在gcc中使用std :: bind編譯錯誤4.7
據我瞭解,下面的基本使用std::bind
應該很好地工作:
#include <functional>
int foo(int a, int b){ return a+b; }
int main(){
using namespace std::placeholders;
// works
auto bar_auto=std::bind(foo,1,_2);
// compile error
std::function<int(int)> bar_fun=std::bind(foo,1,_2);
int quux=1;
// compile error
std::function<int(int)> bar_fun_lvalue=std::bind(foo,quux,_2);
}
肯定的bar_auto
類型爲std::function<int(int)>
(1個int
參數約束型的foo
),所以爲什麼bar_fun
失敗編譯?我包含bar_fun_lvalue
,因爲一些Google搜索向我顯示rvalues used to be problematic。但是這並沒有解決任何問題。
它類似於this bug,但那已經太舊了,我不認爲它是相關的。
gcc的輸出功率沒有特別啓發:
In file included from bindnew.cpp:1:0: /usr/include/c++/4.7/functional: In instantiation of ‘static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = int; _Functor = std::_Bind))(int, int)>; _ArgTypes = {int}]’: /usr/include/c++/4.7/functional:2298:6: required from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = std::_Bind))(int, int)>; _Res = int; _ArgTypes = {int}; typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function::_Useless]’ bindnew.cpp:15:52: required from here /usr/include/c++/4.7/functional:1912:40: error: no match for call to ‘(std::_Bind))(int, int)>) (int)’ /usr/include/c++/4.7/functional:1140:11: note: candidates are: /usr/include/c++/4.7/functional:1211:2: note: template _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = int (*)(int, int); _Bound_args = {int, std::_Placeholder<2>}] /usr/include/c++/4.7/functional:1211:2: note:
template argument deduction/substitution failed: /usr/include/c++/4.7/functional:1206:35: error: cannot convert ‘std::_No_tuple_element’ to ‘int’ in argument passing /usr/include/c++/4.7/functional:1225:2: note: template _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = int (*)(int, int); _Bound_args = {int, std::_Placeholder<2>}] /usr/include/c++/4.7/functional:1225:2: note:
template argument deduction/substitution failed: /usr/include/c++/4.7/functional:1219:35: error: cannot convert ‘std::_No_tuple_element’ to ‘int’ in argument passing /usr/include/c++/4.7/functional:1239:2: note: template _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = int (*)(int, int); _Bound_args = {int, std::_Placeholder<2>}] /usr/include/c++/4.7/functional:1239:2: note:
template argument deduction/substitution failed: /usr/include/c++/4.7/functional:1233:35: error: cannot convert ‘std::_No_tuple_element’ to ‘int’ in argument passing /usr/include/c++/4.7/functional:1253:2: note: template _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = int (*)(int, int); _Bound_args = {int, std::_Placeholder<2>}] /usr/include/c++/4.7/functional:1253:2: note: template argument deduction/substitution failed: /usr/include/c++/4.7/functional:1247:35: error: cannot convert ‘std::_No_tuple_element’ to ‘int’ in argument passing
謝謝,這清除了事情。這會對'bar_auto'的編譯器警告有幫助。有沒有什麼理由讓我的這種愚蠢的錯誤不會觸發警告? –
@MarcClaesen好吧,它有一種長時間和無法讀取的錯誤。 :) –
雖然這個轉儲不是關於'bar_auto'的。我猜沒有警告,因爲'std :: bind'的結果函數可能有任意數量的參數,所以無法評估給定的佔位符是否不合適(在我的例子中是'_2')。 –