jeudi 25 février 2016

Ambiguous call to overloaded constructor with Boost 1.60 and Visual Studio 2013

Consider the following:

#include <boost/make_shared.hpp>

class Foo {};
class Derived : public Foo {};

class Bar {
public:
    explicit Bar( boost::shared_ptr<Foo> ) {}
    explicit Bar( unsigned ) {}
};

int main() {
    boost::shared_ptr<Derived> derived;
    Bar b( derived );
}

With compiling with Visual Studio 2013 and Boost 1.60, this code results in the following:

error C2668: 'Bar::Bar' : ambiguous call to overloaded function
    could be 'Bar::Bar(const Bar &)'
    or       'Bar::Bar(unsigned int)'
    or       'Bar::Bar(boost::shared_ptr<Foo>)'
while trying to match the argument list '(boost::shared_ptr<Derived>)'

I can't imagine why the compiler can't disambiguate the two constructors for Bar... What's going on here?

The code above compiles fine using any of the following:

  • Prior versions of visual studio (I tested the v90 toolset from VS2008)
  • Prior versions of boost (I tested v1.50)
  • GCC 4.8.3

Also note that this problem does not occur with similarly overloaded member or free functions. I only see the issue with constructors.

I can workaround the issue by explicitly up-casting the pointer:

Bar b( boost::static_pointer_cast<Foo>( derived ) );

But I'd really like to know what's going on. Compiler bug? Boost 1.60 bug? Some wacky implicit conversion going on?

Aucun commentaire:

Enregistrer un commentaire