【问题标题】:C++0x Error: overloading a function with std::shared_ptr to const argument is ambiguousC++0x 错误:将带有 std::shared_ptr 的函数重载为 const 参数不明确
【发布时间】:2011-09-13 10:07:44
【问题描述】:

假设我有两个 不相关AB。我还有一个类Bla 使用boost::shared_ptr,如下所示:

class Bla {
public:
    void foo(boost::shared_ptr<const A>);
    void foo(boost::shared_ptr<const B>);
}

注意 const。这是这个问题的原始版本缺少的重要部分。这编译,下面的代码工作:

Bla bla;
boost::shared_ptr<A> a;
bla.foo(a);

但是,如果我在上述示例中从使用 boost::shared_ptr 切换到使用 std::shared_ptr,我会收到如下编译错误:

"error: call of overloaded 'foo(std::shared_ptr<A>)' is ambiguous
note: candidates are: void foo(std::shared_ptr<const A>)
                      void foo(std::shared_ptr<const B>)

你能帮我弄清楚为什么编译器无法确定在 std::shared_ptr 情况下使用哪个函数,而在 boost::shared_ptr 情况下可以吗?我正在使用 Ubuntu 11.04 软件包存储库中的默认 GCC 和 Boost 版本,目前是 GCC 4.5.2 和 Boost 1.42.0。

这是您可以尝试编译的完整代码:

#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
// #include <memory>
// using std::shared_ptr;

class A {};
class B {};

class Bla {
public:
    void foo(shared_ptr<const A>) {}
    void foo(shared_ptr<const B>) {}
};

int main() {
    Bla bla;
    shared_ptr<A> a;

    bla.foo(a);

    return 0;
}

顺便说一句,这个问题促使我向this question 询问我是否应该使用std::shared_ptr ;-)

【问题讨论】:

  • 什么编译器(版本)?
  • 也许您包含更多代码和#include 路径等?
  • 原始问题不正确,因为它在 shared_ptr 的模板类型中缺少“const”。我解决了这个问题并提供了完整的代码示例。
  • 您使用的是哪个版本的 Boost?
  • @Lex 我可以重现该问题。但是,在切换到更新的 GCC 时它不存在。也许您可能想测试 gcc-snapshot,或者手动安装 GCC 4.6。

标签: c++ boost stl overloading shared-ptr


【解决方案1】:

shared_ptr 有一个模板单参数构造函数,这里考虑转换。这就是允许在需要 shared_ptr&lt;Base&gt; 的地方提供实际参数 shared_ptr&lt;Derived&gt; 的原因。

由于shared_ptr&lt;const A&gt;shared_ptr&lt;const B&gt; 都有这种隐式转换,所以它是模棱两可的。

至少在 C++0x 中,标准要求 shared_ptr 使用一些 SFINAE 技巧来确保模板构造函数只匹配实际可以转换的类型。

签名是(见[util.smartptr.shared.const]部分):

shared_ptr<T>::shared_ptr(const shared_ptr<T>& r) noexcept;
template<class Y> shared_ptr<T>::shared_ptr(const shared_ptr<Y>& r) noexcept;

要求:除非Y* 可以隐式转换为T*,否则第二个构造函数不得参与重载决议。

可能该库尚未更新以符合该要求。您可以尝试更新版本的 libc++。

Boost 不起作用,因为它缺少该要求。

这是一个更简单的测试用例:http://ideone.com/v4boA(这个测试用例在符合标准的编译器上会失败,如果它编译成功,则意味着原始用例将被错误地报告为不明确。)

VC++ 2010 做对了(对于std::shared_ptr)。

【讨论】:

  • 通常情况下,shared_ptr 的构造函数具有这样的 SFINAE 构造与std::is_convertible。不相关的类不应该是可转换的。
  • @Xeo:标准要求 EXACT 签名。没有 SFINAE 正在进行。还是我错过了什么?好的,是的,我错过了一些东西。该签名不是转换构造函数,它有两个参数。重要的是下一个。
  • @Ben:我还应该阅读编辑后的答案,而不仅仅是您的评论。 :P 我浏览了标准以找到相关部分,但您已经这样做了。 +1
  • @Xeo: std::shared_ptr 在 C++0x 中是新的,不是吗?所以没有必要考虑 C++03 的措辞。并且 Boost 没有隐式可转换要求。
  • @Ben:嗯,C++0x 应该已经成为标准了,因为我已经这样称呼它了……当然是指 FDIS。
【解决方案2】:

以下内容可以在 GCC 4.5 和 Visual Studio 10 中正常编译。如果您说它不能在 GCC 4.5.2 中编译,那么这听起来像是您应该报告的编译器错误(但请确保它确实发生了它更有可能你犯了某种错字)。

#include <memory>
class A{};
class B{};
class Bla {
public:
    void foo(std::shared_ptr<A>) {}
    void foo(std::shared_ptr<B>) {}
};

int main()
{
    Bla bla;
    std::shared_ptr<A> a;
    bla.foo(a);
}

【讨论】:

  • 它也可以在 Visual C++ 2010 SP1 中正常编译。
  • 我也可以用 GCC 4.5 确认这一点。
  • 谢谢你们。这回答了应该有shared_ptr&lt;A&gt; 而不是shared_ptr&lt;const A&gt; 的原始问题。它确实与boost::std:: 版本一起编译。正如修改后的问题现在解释的那样,当添加 const 时问题就来了。
【解决方案3】:

您可以使用std::static_pointer_cast 来添加const 资格:

bla.foo(std::static_pointer_cast<const A>(a));

【讨论】:

  • 自动转换。请参阅@Ben 的回答。
  • @Xeo:删除了该断言,只保留解决方案(或解决方法,如果您愿意)。
【解决方案4】:

http://bytes.com/topic/c/answers/832994-shared_ptr-derived-classes-ambiguitity-overloaded-functions

struct yes_type { char dummy; };
struct no_type { yes_type a; yes_type b; };

template < typename From, typename To >
class is_convertible
{
    private:
        static From* dummy ( void );

        static yes_type check ( To );

        static no_type check ( ... );

    public:

        static bool const value = sizeof( check( *dummy() ) ) == sizeof( yes_type );

}; // is_convertible

在boost的shared_ptr.h中,将构造函数签名改为:

template<class Y>
shared_ptr(shared_ptr<Y> const & r,
    typename enable_if<is_convertible<Y*, T*>::value, void*>::type = 0
    ): px(r.px), pn(r.pn) // never throws
{
}

【讨论】:

    猜你喜欢
    • 2015-07-18
    • 2011-09-27
    • 2011-09-12
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多