How to force template function overload for boost :: bind?

I am trying to create a predicate for std::find_ifusing boost::bindalong with boost::contains(from boost / algoritm / string library). The following snippet shows two ways I am trying to accomplish this.

#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;
}

When compiling this code with g ++ 3.4.5, I get the following output.

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)'

When I switch to boost::icontainswhich has only one overload, everything works fine. I know how to solve a similar situation when there are several function overloads without a template. Can someone help me write this correctly? Or should I write my own comparison function?

+5
2

static_cast<bool(*)(const std::string&, const std::string&)>(&boost::contains<..>), .

, . , , boost:: bind.

- ++ 0x, .

+8

( + ) , Visual Studio 2008 ( Microsoft).

gcc.

0

All Articles