如何强制 boost::bind 的模板函数重载?

发布于 2024-08-22 13:29:57 字数 1819 浏览 8 评论 0原文

我试图通过使用 boost::bindboost::contains (来自 boost/algoritm)为 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 时,一切正常。 我知道当非模板函数有多个重载时如何解决类似的情况。 有人可以帮我正确地写这个吗?或者我应该编写自己的比较函数?

I'm trying to create predicate for std::find_if by using boost::bind together with boost::contains (from boost/algoritm/string library).
Following snippet shows two ways how I'm 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'm getting 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::icontains which has only one overload everyting works fine.
I know how to solve similar situation when there are multiple overloads of non-template function.
Can someone help me write this correctly? Or should I write my own compare function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2024-08-29 13:29:57

您需要编写 static_cast(&boost::contains<..>) 来解析超载。

是的,这是模板和重载带来的巨大痛苦。使用 OOP 和重载编写的库很难与模板和 boost::bind 一起使用。

我们都在等待 C++0x lambda 表达式,它应该可以更好地解决问题。

You need to write static_cast<bool(*)(const std::string&, const std::string&)>(&boost::contains<..>) to resolve the overload.

Yes, this is a royal pain with templates and overloading. Libs written with OOP and overloading in mind are difficult to use with templates and boost::bind.

We all wait for C++0x lambda expressions, which ought to resolve things better.

耳根太软 2024-08-29 13:29:57

该代码对我来说看起来很好(正确+兼容),并且它使用 Visual Studio 2008 进行编译(禁用 Microsoft 语言扩展)。

尝试使用更新版本的 gcc。

That code looks fine (correct + compliant) to me, and it compiles using Visual Studio 2008 (with Microsoft language extensions disabled).

Try using a more up to date version of gcc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文