Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ambiguous

Reply
Thread Tools

ambiguous

 
 
Tim H
Guest
Posts: n/a
 
      06-15-2007
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

 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      06-15-2007
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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-15-2007
On Jun 15, 9:27 am, Alan Johnson <(E-Mail Removed)> 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:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ambiguous reference to type jahaya@gmail.com VHDL 2 09-12-2005 09:55 AM
'CrystalReportViewer' is ambiguous in the namespace 'CrystalDecisions. Nancy. ASP .Net 3 09-28-2004 08:21 PM
70-300, ambiguous question Session MCSD 2 05-04-2004 05:55 PM
Ambiguous type? Tuukka Toivonen VHDL 3 05-03-2004 02:36 PM
Parser Error Message: Ambiguous match found. Denon ASP .Net 2 01-02-2004 09:19 AM



Advertisments