c++ - std::[tr1::]ref 和 boost::ref 之间的混淆

标签 c++ boost bind tr1

注意:这是 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/

相关文章:

c++ - Boost Log运行时优化

javascript - 匿名函数bind的removeEventListener

c++ - 在 MongoDB 中查询字符串的中间部分?

C++ 无法理解的崩溃行为

c++ - 处理使用 boost::interprocess STL 兼容共享内存分配器创建的 C++ 对象的正确方法是什么?

memory - 将 tmpfs 或 ramfs 绑定(bind)到特定的内存节点

bind - fusionrmount 调用类似于本地目录的 mount --bind ?

c++ - 哪些库对 std::make_shared 使用了 "We Know Where You Live"优化?

c++ - 从轮廓 OpenCV 中提取矩形

c++ - Boost.Regex 分隔符解析