confusion between std::[tr1::]ref and boost::ref

自作多情 提交于 2019-12-10 13:43:10

问题


Beware: This is GCC 4.1.2. We're on a proprietary embedded platform. We cannot update to a new compiler. So C++03 + TR1 it is.

We somewhere have a function like this:

template<typename T>
void foo(const boost::any& x)
{
  bar(boost::any_cast<T>(x));
}

which is then later used in a bind expression:

std::tr1::bind( &foo<T>, _1);

This generates the following error:

error: call of overloaded 'ref(const boost::any&)' is ambiguous
note: candidates are: std::tr1::reference_wrapper<_Tp> std::tr1::ref(_Tp&) [with _Tp = const boost::any]
note: const boost::reference_wrapper boost::ref(T&) [with T = const boost::any]

I do understand that this is Koenig lookup hitting us. However, I lack ideas about how to circumvent this problem.

Anyone out there?


回答1:


Define a version of boost::ref() specifically taking a boost::any and have it return the proper type. Probably

namespace boost {
    std::tr1::reference_wrapper<boost::any const>
    ref(boost::any const& o) {
        return std::tr1::ref(o);
    }
}


来源:https://stackoverflow.com/questions/20546860/confusion-between-stdtr1ref-and-boostref

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!