By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

ambiguous

P: n/a
Why is this ambiguous:

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

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

void do_something(const boost::shared_ptr<otherdev) {}
void do_something(const boost::shared_ptr<basescope) {}

int main()
{
boost::shared_ptr<baseb(new base());
boost::shared_ptr<derivedd(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<basescope) {}

int main()
{
boost::shared_ptr<baseb(new base());
boost::shared_ptr<derivedd(new derived());

do_something(b);
do_something(d);

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

Thanks!

Tim

Jun 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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<otherdev) {}
void do_something(const boost::shared_ptr<basescope) {}

int main()
{
boost::shared_ptr<baseb(new base());
boost::shared_ptr<derivedd(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<baseany more than
std::vector<derivedwould 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
Jun 15 '07 #2

P: n/a
On Jun 15, 9:27 am, Alan Johnson <a...@yahoo.comwrote:
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<otherdev) {}
void do_something(const boost::shared_ptr<basescope) {}
int main()
{
boost::shared_ptr<baseb(new base());
boost::shared_ptr<derivedd(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:ja*********@gmail.com
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

Jun 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.