区分boost::bind中同名的const和非常量方法

发布于 2024-08-22 01:18:39 字数 175 浏览 2 评论 0原文

当我使用 boost::bind 和声明为 const 和非 const 的方法名称时,我遇到了不明确的错误,例如

boost::bind( &boost::optional<T>::get, _1 )

如何解决这个问题?

When I use boost::bind with a method name which is declared both const and non-const I am getting in ambiguous error, for example

boost::bind( &boost::optional<T>::get, _1 )

How can I solve this problem?

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

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

发布评论

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

评论(1

趁微风不噪 2024-08-29 01:18:39

Boost 的常见问题解答部分描述了该问题以及解决方法。绑定引用。

您还可以使用如下实用函数:

#include <boost/bind.hpp>
#include <boost/optional.hpp>

template <class Ret, class Obj>
Ret (Obj::* const_getter(Ret (Obj::*p) () const)) () const
{
    return p;
}

template <class Ret, class Obj>
Ret (Obj::* nonconst_getter(Ret (Obj::*p)())) ()
{
    return p;
}

int main()
{
    boost::bind( const_getter(&boost::optional<int>::get), _1 );
    boost::bind( nonconst_getter(&boost::optional<int>::get), _1 );
}

The problem together with workarounds is descibed in FAQ part of Boost.Bind reference.

You could also make use of utility functions like the following:

#include <boost/bind.hpp>
#include <boost/optional.hpp>

template <class Ret, class Obj>
Ret (Obj::* const_getter(Ret (Obj::*p) () const)) () const
{
    return p;
}

template <class Ret, class Obj>
Ret (Obj::* nonconst_getter(Ret (Obj::*p)())) ()
{
    return p;
}

int main()
{
    boost::bind( const_getter(&boost::optional<int>::get), _1 );
    boost::bind( nonconst_getter(&boost::optional<int>::get), _1 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文