मैं boost::bind
का उपयोग boost::contains
(बूस्ट/एल्गोरिदम/स्ट्रिंग लाइब्रेरी से) के साथ std::find_if
के लिए भविष्यवाणी करने की कोशिश कर रहा हूं। निम्नलिखित स्निपेट दो तरीकों से पता चलता है कि मैं इसे कैसे पूरा करने की कोशिश कर रहा हूं।बूस्ट :: बाइंड के लिए टेम्पलेट फ़ंक्शन अधिभार को कैसे बल दें?
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <string>
int main(int argc, char** argv) {
std::string s1("hello mom");
std::string s2("bye mom");
boost::function<bool (std::string, std::string)> f = &boost::contains<std::string, std::string>;
std::cout << s1 << " contains " << "hello, " << std::boolalpha << f(s1, "hello") << std::endl;
std::cout << s2 << " contains " << "hello, " << std::boolalpha << f(s2, "hello") << std::endl;
boost::function<bool (std::string)> contain_hello = boost::bind(boost::contains<std::string, std::string>, _1, std::string("hello"));
std::cout << s1 << " contains " << "hello, " << std::boolalpha << contain_hello(s1) << std::endl;
std::cout << s2 << " contains " << "hello, " << std::boolalpha << contain_hello(s2) << std::endl;
return EXIT_SUCCESS;
}
इस कोड को g ++ 3.4.5 के साथ संकलित करते समय मुझे निम्नलिखित आउटपुट मिल रहा है।
error: conversion from `<unresolved overloaded function type>' to non-scalar type `boost::function<bool()(std::string, std::string), std::allocator<void> >' requested
error: no matching function for call to `bind(<unresolved overloaded function type>, boost::arg<1>&, std::string)'
जब मैं boost::icontains
जो केवल एक अधिभार everyting ठीक काम करता है है पर स्विच करें। मुझे पता है कि गैर-टेम्पलेट फ़ंक्शन के एकाधिक ओवरलोड होने पर समान स्थिति को कैसे हल किया जाए। क्या कोई मुझे सही तरीके से लिखने में मदद कर सकता है? या मुझे अपना खुद का तुलना फ़ंक्शन लिखना चाहिए?
छोटे सुधार (कम से कम जी ++ 3.4.5) static_cast (boost :: <..> है)। अब यह पूरी तरह से काम करता है। त्वरित संकेत के लिए बहुत बहुत धन्यवाद। –
lollinus
ठीक है। धन्यवाद। मैंने इसके साथ जवाब अपडेट कर लिया है। मैंने आमतौर पर इसे सदस्य-फ़ंक्शन ptrs के साथ उपयोग किया है, इसलिए मेरा नियमित func-ptr वाक्यविन्यास स्पष्ट रूप से थोड़ा जंगली था। ;) – Macke
टेम्पलेट्स और ओवरलोडिंग की पिटा प्रकृति पर आपकी फ्रैंकनेस बहुत आश्वस्त है। इसे देखने से पहले, मैंने सोचा कि मैं कुछ गलत कर रहा होगा। –