C++0x Error: overloading a function with std::shared_ptr to const argument is ambiguous

后端 未结 4 557
醉梦人生
醉梦人生 2021-01-18 09:04

Suppose I have two unrelated classes A and B. I also have a class Bla that uses boost::shared_ptr like t

4条回答
  •  伪装坚强ぢ
    2021-01-18 09:51

    shared_ptr has a template single-argument constructor, which is considered for the conversion here. That's what allows an actual parameter shared_ptr to be supplied where a shared_ptr is needed.

    Since both shared_ptr and shared_ptr have this implicit conversion, it's ambiguous.

    At least in C++0x, the standard requires that shared_ptr use some SFINAE tricks to make sure that the template constructor only matches types that actually can be converted.

    The signature is (see section [util.smartptr.shared.const]):

    shared_ptr::shared_ptr(const shared_ptr& r) noexcept;
    template shared_ptr::shared_ptr(const shared_ptr& r) noexcept;
    

    Requires: The second constructor shall not participate in the overload resolution unless Y* is implicitly convertible to T*.

    Possibly the library hasn't yet been updated to comply with that requirement. You might try a newer version of libc++.

    Boost won't work, because it's missing that requirement.

    Here's a simpler test case: http://ideone.com/v4boA (This test case will fail on a conforming compiler, if it compiles successfully, it means the original case will be incorrectly reported as ambiguous.)

    VC++ 2010 gets it right (for std::shared_ptr).

提交回复
热议问题