簡體   English   中英

在boost :: bind中區分具有相同名稱的const和非const方法

[英]Distinguish between const and non-const method with same name in boost::bind

當我將boost::bind與同時聲明為const和非const的方法名一起使用時,我遇到了模棱兩可的錯誤,例如

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

我怎么解決這個問題?

Boost.Bind參考的FAQ部分中描述了該問題以及解決方法。

您還可以使用以下實用工具功能:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM