मुझे यह पता लगाने में परेशानी हो रही है कि बिल्ली यह संकलन क्यों नहीं कर रहा है। मुझे कुछ लैम्ब्डा फ़ंक्शन मिला है जो कुछ तर्क के आधार पर std::function
देता है।std :: बाध्य फ़ंक्शन बाध्य करें
मैं इस स्निपेट को अपनी समस्या को संकुचित होता है (जो lambdas का उपयोग नहीं करता है, लेकिन पूरी तरह से मेरी त्रुटि reproduces):
#include <functional>
#include <iostream>
struct foo {
template<class T>
void bar(T data) {
std::cout << data << "\n";
}
};
void some_fun(const std::function<void(int)> &f) {
f(12);
}
int main() {
foo x;
auto f = std::bind(&foo::bar<int>, x, std::placeholders::_1);
auto w = std::bind(some_fun, f);
w();
}
w()
करने के लिए कॉल उन सुंदर जीसीसी त्रुटि आउटपुट में से एक का उत्पादन है, जिसमें मैं यह नहीं समझ सकता कि क्या गलत हो रहा है।
g++ -std=c++0x test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:20:7: error: no match for call to ‘(std::_Bind<void (*(std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>))(const std::function<void(int)>&)>)()’
/usr/include/c++/4.6/functional:1130:11: note: candidates are:
/usr/include/c++/4.6/functional:1201:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function<void(int)>&), _Bound_args = {std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>}]
/usr/include/c++/4.6/functional:1215:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function<void(int)>&), _Bound_args = {std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>}]
/usr/include/c++/4.6/functional:1229:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function<void(int)>&), _Bound_args = {std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>}]
/usr/include/c++/4.6/functional:1243:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}, _Result = _Result, _Functor = void (*)(const std::function<void(int)>&), _Bound_args = {std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>}]
यहाँ, f
कुछ प्रतिदेय वस्तु जो तर्क के रूप में एक पूर्णांक लेता है और इसे का उपयोग कॉल किया जाना चाहिए: यह जीसीसी 4.6.1 द्वारा गूँजती त्रुटि है। दूसरी तरफ, w
केवल एक कॉल करने योग्य ऑब्जेक्ट है जो some_fun(f)
पर कॉल करता है, f
ऊपर उल्लिखित कॉल करने योग्य ऑब्जेक्ट होने पर, some_fun
के पैरामीटर द्वारा हस्ताक्षर अपेक्षित है।
क्या मुझे कुछ याद आ रही है? मुझे शायद यह नहीं पता कि वास्तव में std::bind
और std::function
कैसे मिलाएं।
यह है जब आप 'std :: समारोह' च के लिए के साथ ऑटो की जगह काम करने के लिए लगता है। –
Lalaland
यह आपकी समस्या का उत्तर नहीं हो सकता है .. लेकिन आप देशी लैम्ब्डा-कार्यों सी ++ 11 (जो कर एसटीडी अनावश्यक बाँध ::) के साथ आ रहा है का उपयोग कर विचार किया है? – coldfix
बूस्ट में हमारे पास इस तरह के मामलों के लिए 'रक्षा' है, लेकिन ऐसा लगता है कि यह मानक को नहीं बना रहा है। –