472,952 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

ambiguous

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
2 1843
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: REH | last post by:
Can some tell me why the chooses the constructor in class B over operator B in class A? Is this not ambiguous? Thanks. #include <iostream> using namespace std;
1
by: Alan Johnson | last post by:
Consider the following code, with the interesting lines numbered in comments: class A { public : bool f(int level = 1) // line 5 { return true ; }
9
by: xuatla | last post by:
compile error: test1.cpp:21: error: ISO C++ says that `T mtd::CDiffOperator::getdp(const mtd::mVector&, long int, mtd::mBCTYPE) const' and `void mtd::CDiffOperator::getdp(mtd::mVector&, const...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
3
by: Arpan | last post by:
The following code exists in a class file named "Users.vb": Namespace Users Public Class UserDetails Public FirstName As String Public LastName As String Public UserName As String Public...
1
by: rn5a | last post by:
Consider the following code in a VB class file: Namespace LoginUserFetchDB Public Class ZForZebra : Inherits SoapHeader Public UserName As String Public Password As String End Class Public...
8
by: xtrigger303 | last post by:
Hi to all, I'm working on a smart pointer implementation and I'm trying to get automatic type conversion between different pointer types. I stumbled upon something weird (at least for me) I...
12
by: Nathan Sokalski | last post by:
I have several CustomControls that I have written for my project. However, when I try to compile I recieve the following warning & errors: Warning 32 Could not resolve this reference. Could not...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.