ambiguous

Discussion in 'C++' started by Tim H, Jun 15, 2007.

  1. Tim H

    Tim H Guest

    Why is this ambiguous:

    ------------------------------------------------
    #include <boost/shared_ptr.hpp>

    class base {};
    class derived: public base {};
    class other {};

    void do_something(const boost::shared_ptr<other> dev) {}
    void do_something(const boost::shared_ptr<base> scope) {}

    int main()
    {
    boost::shared_ptr<base> b(new base());
    boost::shared_ptr<derived> d(new derived());

    do_something(b);
    do_something(d);

    return 0;
    }
    ------------------------------------------------

    but this is not

    ------------------------------------------------
    class base {};
    class derived: public base {};
    class other {};

    void do_something(const other *dev) {}
    void do_something(const base *scope) {}

    int main()
    {
    base *b = new base();
    derived *d = new derived();

    do_something(b);
    do_something(d);

    return 0;
    }
    -------------------------------------------------------

    and this is also allowed

    ------------------------------------------------------
    #include <boost/shared_ptr.hpp>

    class base {};
    class derived: public base {};
    class other {};

    void do_something(const boost::shared_ptr<base> scope) {}

    int main()
    {
    boost::shared_ptr<base> b(new base());
    boost::shared_ptr<derived> d(new derived());

    do_something(b);
    do_something(d);

    return 0;
    }
    --------------------------------------------------------------------

    Thanks!

    Tim
    Tim H, Jun 15, 2007
    #1
    1. Advertising

  2. Tim H

    Alan Johnson Guest

    Tim H wrote:
    > Why is this ambiguous:
    >
    > ------------------------------------------------
    > #include <boost/shared_ptr.hpp>
    >
    > class base {};
    > class derived: public base {};
    > class other {};
    >
    > void do_something(const boost::shared_ptr<other> dev) {}
    > void do_something(const boost::shared_ptr<base> scope) {}
    >
    > int main()
    > {
    > boost::shared_ptr<base> b(new base());
    > boost::shared_ptr<derived> d(new derived());
    >
    > do_something(b);
    > do_something(d);
    >
    > return 0;
    > }
    > ------------------------------------------------
    >
    > but this is not
    >
    > ------------------------------------------------
    > class base {};
    > class derived: public base {};
    > class other {};
    >
    > void do_something(const other *dev) {}
    > void do_something(const base *scope) {}
    >
    > int main()
    > {
    > base *b = new base();
    > derived *d = new derived();
    >
    > do_something(b);
    > do_something(d);
    >
    > return 0;
    > }
    > -------------------------------------------------------
    >


    In your second example, the type 'derived *' has relationship to the
    type 'base *' that is well defined by the standard. Specifically,
    pointers to derived are convertible to pointers to base.

    In your first example, the type 'shared_ptr<derived>' has no particular
    relation to the type 'shared_ptr<base>'. That is, shared_ptr<derived>
    is not convertible to shared_ptr<base> any more than
    std::vector<derived> would be convertible to std::vector<base>.

    shared_ptr does have a template constructor, though, that can accept
    type shared_ptr<T>. The problem from the compiler's perspective is
    whether it should instantiate that template with T = base or T = other,
    thus, ambiguity.

    --
    Alan Johnson
    Alan Johnson, Jun 15, 2007
    #2
    1. Advertising

  3. Tim H

    James Kanze Guest

    On Jun 15, 9:27 am, Alan Johnson <> wrote:
    > Tim H wrote:
    > > Why is this ambiguous:


    > > ------------------------------------------------
    > > #include <boost/shared_ptr.hpp>


    > > class base {};
    > > class derived: public base {};
    > > class other {};


    > > void do_something(const boost::shared_ptr<other> dev) {}
    > > void do_something(const boost::shared_ptr<base> scope) {}


    > > int main()
    > > {
    > > boost::shared_ptr<base> b(new base());
    > > boost::shared_ptr<derived> d(new derived());


    > > do_something(b);
    > > do_something(d);


    > > return 0;
    > > }
    > > ------------------------------------------------


    [...]
    > shared_ptr does have a template constructor, though, that can accept
    > type shared_ptr<T>. The problem from the compiler's perspective is
    > whether it should instantiate that template with T = base or T = other,
    > thus, ambiguity.


    Just a small addition to a very good explination: the important
    point to keep in mind is that the compiler does overload
    resolution before it instantiates the chosen template function.
    So it cannot know that the conversion will cause an error if it
    is instantiated with other.

    There is a proposal (more than one, in fact) to add concepts to
    the language. As I understand it, this will allow expression of
    requirements like the fact that the template conversion
    constructor requires the pointers to the underlying types to be
    implicitly convertable. I'm not really sure of the details, but
    presumably, such constraints will play a role in template type
    deduction, ensuring that the conversion to other will not be
    instantiated here, and thus that overload resolution will work.
    So maybe sometime in the future...

    --
    James Kanze (GABI Software, from CAI) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 15, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tuukka Toivonen

    Ambiguous type?

    Tuukka Toivonen, Apr 30, 2004, in forum: VHDL
    Replies:
    3
    Views:
    722
    Stefan Frank
    May 3, 2004
  2. Replies:
    2
    Views:
    977
  3. Denon
    Replies:
    2
    Views:
    4,374
    Yuke Liew
    Jan 2, 2004
  4. Nancy.
    Replies:
    3
    Views:
    8,107
    i. Wiin
    Sep 28, 2004
  5. Ben

    Ambiguous match found.

    Ben, Apr 27, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    7,904
    bond007
    Mar 23, 2010
Loading...

Share This Page