【问题标题】:confusion between std::[tr1::]ref and boost::refstd::[tr1::]ref 和 boost::ref 之间的混淆
【发布时间】:2023-03-18 00:45:01
【问题描述】:

注意:这是 GCC 4.1.2。我们在一个专有的嵌入式平台上。我们无法更新到新的编译器。所以是 C++03 + TR1。

我们在某处有这样的功能:

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

然后在绑定表达式中使用:

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

这会产生以下错误:

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]

我明白这是 Koenig 查找对我们的打击。但是,我缺乏关于如何规避这个问题的想法。

外面有人吗?

【问题讨论】:

  • 您的代码中是否有意缺少ref
  • @Simple:你在哪里看到缺少ref
  • 您的两个代码示例没有调用ref,而错误却调用了。我问这是故意的还是无意的。你提到的 ADL 我不确定。
  • @Simple:在std::tr1::bind 的实现中,对ref() 的无限制调用是使用boost::any 作为参数进行的。 Koenig 查找似乎使 boost::ref()std::tr1::ref() 一样好匹配此调用。因此错误。无论如何,Dietmar 采用特殊重载的好主意解决了这个问题。
  • @sbi:实现似乎使用了不合格的ref(),这似乎很奇怪。如果它使用ref(),它应该真的有资格。

标签: c++ boost bind tr1


【解决方案1】:

定义一个boost::ref() 的版本,专门采用boost::any 并让它返回正确的类型。可能

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

【讨论】:

    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2011-08-15
    • 2012-08-27
    相关资源
    最近更新 更多