मेरे पास एक ऐसा फ़ंक्शन है जो एक कंटेनर पर पुनरावृत्त करता है और फ़िल्टरिंग के लिए प्रत्येक तत्व को भविष्य में पास करता है। इस फ़ंक्शन का अधिभार प्रत्येक तत्व की अनुक्रमणिका को भविष्य में भी पास करता है।क्या सी ++ लैम्बडा सही ढंग से अधिभारित फ़ंक्शंस का चयन नहीं करते हैं?
template<typename TContainer>
void DoSomethingIf(TContainer &c, std::function<bool (const typename TContainer::const_reference)> predicate);
template<typename TContainer>
void DoSomethingIf(TContainer &c, std::function<bool (const typename TContainer::const_reference, int)> predicate);
मैं ने पाया है कि एक नग्न लैम्ब्डा साथ या तो इन कार्यों के फोन करने का प्रयास, VC11 में एक संकलक त्रुटि का कारण एक std :: समारोह वस्तु का उपयोग करते समय सफल होगा होगा:
void foo()
{
std::vector<int> v;
// fails
DoSomethingIf(v, [](const int &x) { return x == 0; });
// also fails
auto lambda = [](const int &x) { return x == 0; };
DoSomethingIf(v, lambda);
// success!
std::function<bool (const int &)> fn = [](const int &x) { return x == 0; };
DoSomethingIf(v, fn);
}
1>c:\users\moswald\test.cpp(15): error C2668: 'DoSomethingIf' : ambiguous call to overloaded function
1> c:\users\moswald\test.cpp(8): could be 'void DoSomethingIf<std::vector<_Ty>>(TContainer &,std::function<_Fty>)'
1> with
1> [
1> _Ty=int,
1> TContainer=std::vector<int>,
1> _Fty=bool (const int &,int)
1> ]
1> c:\users\moswald\test.cpp(5): or 'void DoSomethingIf<std::vector<_Ty>>(TContainer &,std::function<_Fty>)'
1> with
1> [
1> _Ty=int,
1> TContainer=std::vector<int>,
1> _Fty=bool (const int &)
1> ]
1> while trying to match the argument list '(std::vector<_Ty>, foo::<lambda_8EADDE04A8D35A3C>)'
1> with
1> [
1> _Ty=int
1> ]
1>c:\users\moswald\test.cpp(19): error C2668: 'DoSomethingIf' : ambiguous call to overloaded function
1> c:\users\moswald\test.cpp(8): could be 'void DoSomethingIf<std::vector<_Ty>>(TContainer &,std::function<_Fty>)'
1> with
1> [
1> _Ty=int,
1> TContainer=std::vector<int>,
1> _Fty=bool (const int &,int)
1> ]
1> c:\users\moswald\test.cpp(5): or 'void DoSomethingIf<std::vector<_Ty>>(TContainer &,std::function<_Fty>)'
1> with
1> [
1> _Ty=int,
1> TContainer=std::vector<int>,
1> _Fty=bool (const int &)
1> ]
1> while trying to match the argument list '(std::vector<_Ty>, foo::<lambda_8EADDE04A8D35A3D>)'
1> with
1> [
1> _Ty=int
1> ]
क्या इसकी उम्मीद की जानी चाहिए थी? वहाँ इन कार्यों (एक का नाम बदलने "DoSomethingIfWithIndex
" होने के लिए की कमी ओवरलोड एक अलग तरह?
भावना है, लेकिन निराशाजनक बनाता है। ऐसा लगता है कि अलग-अलग लैम्ब्स के माध्यम से ओवरलोडेड फ़ंक्शन का चयन करने का कोई तरीका नहीं है। – moswald
'std :: function' का उपयोग नहीं कर रहा, नहीं। आप कॉल करने योग्य गुणों का उपयोग कर सकते हैं या कैप्चरलेस लैम्बडास के लिए फ़ंक्शन पॉइंटर क्षय पर भरोसा कर सकते हैं, लेकिन आम तौर पर यह बट में दर्द होता है। –
इस तरह कुछ हल करने के लिए कॉल करने योग्य लक्षणों का उपयोग करने के बारे में कोई कैसे होगा? यदि संभव हो, तो मैं किसी भी फ़ंक्शन ऑब्जेक्ट को पास करने में सक्षम होना पसंद करूंगा जो हस्ताक्षर से मेल खाता है, न केवल मुफ्त फ़ंक्शंस/लैम्बडास। – moswald