注意:这是 GCC 4.1.2。我们在专有的嵌入式平台上。我们无法更新到新的编译器。所以 C++03 + TR1 是。
我们在某个地方有这样一个函数:
template<typename T>
void foo(const boost::any& x)
{
bar(boost::any_cast<T>(x));
}
随后在绑定(bind)表达式中使用:
std::tr1::bind( &foo<T>, _1);
这会产生以下错误:
error: call of overloaded 'ref(const boost::any&)' is ambiguous<br/>
note: candidates are: std::tr1::reference_wrapper<_Tp> std::tr1::ref(_Tp&) [with _Tp = const boost::any]<br/>
note: const boost::reference_wrapper boost::ref(T&) [with T = const boost::any]
我知道这是 Koenig 查找命中我们。但是,我缺乏关于如何规避这个问题的想法。
有人在吗?
最佳答案
定义 boost::ref()
的一个版本,专门采用 boost::any
并让它返回正确的类型。大概
namespace boost {
std::tr1::reference_wrapper<boost::any const>
ref(boost::any const& o) {
return std::tr1::ref(o);
}
}
关于c++ - std::[tr1::]ref 和 boost::ref 之间的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20546860/