473,241 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Ambiguous constructor call

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 summarized it in the code
below. I was expecting both things at the end to work or not work at
all....
Any insight?
Thanks in advance,
Francesco
#include <iostream>

class A;

//

class B
{
public:

B() { std::cout << "B()\n"; }

B( B const & ) { std::cout << "B( B const & )\n"; }

~B() { std::cout << "~B()\n"; }

B & operator=( B const & ) { std::cout << "B & operator=( B const & )
\n"; return *this; }

template< typename T >
operator T() const;
};

//

class A
{
public:

A() { std::cout << "A()\n"; }

explicit A( int ) { std::cout << "A( int )\n"; }

A( A const & ) { std::cout << "A( A const & )\n"; }

~A() { std::cout << "~A()\n"; }

A & operator=( A const & ) { std::cout << "A & operator=( A const & )
\n"; return *this; }
};

//

template< typename T >
B::operator T() const { std::cout << "B::operator T() const\n";
return T(); }

//

int main( )
{
B obj001;
A obj002 = obj001; // this works
//A obj003( obj001 ); // this is ambiguous
}

Sep 2 '07 #1
8 3065
On 2007-09-02 19:58, xt*********@gmail.com wrote:
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 summarized it in the code
below. I was expecting both things at the end to work or not work at
all....
Any insight?
Thanks in advance,
Francesco
#include <iostream>

class A;

//

class B
{
public:

B() { std::cout << "B()\n"; }

B( B const & ) { std::cout << "B( B const & )\n"; }

~B() { std::cout << "~B()\n"; }

B & operator=( B const & ) { std::cout << "B & operator=( B const & )
\n"; return *this; }

template< typename T >
operator T() const;
};

//

class A
{
public:

A() { std::cout << "A()\n"; }

explicit A( int ) { std::cout << "A( int )\n"; }

A( A const & ) { std::cout << "A( A const & )\n"; }

~A() { std::cout << "~A()\n"; }

A & operator=( A const & ) { std::cout << "A & operator=( A const & )
\n"; return *this; }
};

//

template< typename T >
B::operator T() const { std::cout << "B::operator T() const\n";
return T(); }

//

int main( )
{
B obj001;
A obj002 = obj001; // this works
//A obj003( obj001 ); // this is ambiguous
}
Since B can be converted to any type you like, it can be converted to
either A or int. Both of which are types for which A has a constructor,
and the compiler have no idea which of them you would like to use. Using
explicit on a constructor only prevents it from being used like this:

A a = 1;

You have to do

A a(1);

--
Erik Wikström
Sep 2 '07 #2
On Sep 2, 8:52 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-02 19:58, xtrigger...@gmail.com wrote:
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 summarized it in the code
below. I was expecting both things at the end to work or not work at
all....
Any insight?
Thanks in advance,
Francesco
#include <iostream>
class A;
//
class B
{
public:
B() { std::cout << "B()\n"; }
B( B const & ) { std::cout << "B( B const & )\n"; }
~B() { std::cout << "~B()\n"; }
B & operator=( B const & ) { std::cout << "B & operator=( B const & )
\n"; return *this; }
template< typename T >
operator T() const;
};
//
class A
{
public:
A() { std::cout << "A()\n"; }
explicit A( int ) { std::cout << "A( int )\n"; }
A( A const & ) { std::cout << "A( A const & )\n"; }
~A() { std::cout << "~A()\n"; }
A & operator=( A const & ) { std::cout << "A & operator=( A const & )
\n"; return *this; }
};
//
template< typename T >
B::operator T() const { std::cout << "B::operator T() const\n";
return T(); }
//
int main( )
{
B obj001;
A obj002 = obj001; // this works
//A obj003( obj001 ); // this is ambiguous
}

Since B can be converted to any type you like, it can be converted to
either A or int. Both of which are types for which A has a constructor,
and the compiler have no idea which of them you would like to use. Using
explicit on a constructor only prevents it from being used like this:

A a = 1;

You have to do

A a(1);

--
Erik Wikström
In fact I was wondering why the first initialization works ( it is not
ambiguous it seems...)
A obj002 = obj001; // this works
while the second doesn't.
//A obj003( obj001 ); // this is ambiguous
I figured that the explicit doesn't help here.
:-)

Sep 2 '07 #3
On Sun, 02 Sep 2007 17:58:17 +0000, xtrigger303 wrote:
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 summarized it in the code
below. I was expecting both things at the end to work or not work at
all....
Any insight?
Thanks in advance,
Francesco
#include <iostream>

class A;

//

class B
{
public:

B() { std::cout << "B()\n"; }

B( B const & ) { std::cout << "B( B const & )\n"; }

~B() { std::cout << "~B()\n"; }

B & operator=( B const & ) { std::cout << "B & operator=( B const
& )
\n"; return *this; }

template< typename T >
operator T() const;
};

//

class A
{
public:

A() { std::cout << "A()\n"; }

explicit A( int ) { std::cout << "A( int )\n"; }

A( A const & ) { std::cout << "A( A const & )\n"; }

~A() { std::cout << "~A()\n"; }

A & operator=( A const & ) { std::cout << "A & operator=( A const
& )
\n"; return *this; }
};

//

template< typename T >
B::operator T() const { std::cout << "B::operator T() const\n"; return
T(); }

//

int main( )
{
B obj001;
A obj002 = obj001; // this works
The standard mandates that this behaves as if a temporary A object is
created first from obj001 and then obj002 is copy constructed from this
temporary object (to further complicate things the actual copy
construction may be elided but the compiler must check that it would have
been possible). Creating the temporary object is an implicit conversion
and hence the explicit A(int) is not considered.
//A obj003( obj001 ); // this is ambiguous
This is an explicit constructor call hence A(int) as well as the copy
constructor are considered.
}
--
Markus Schoder
Sep 3 '07 #4
On Sep 3, 2:44 am, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
On Sun, 02 Sep 2007 17:58:17 +0000, xtrigger303 wrote:
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 summarized it in the code
below. I was expecting both things at the end to work or not work at
all....
Any insight?
Thanks in advance,
Francesco
#include <iostream>
class A;
//
class B
{
public:
B() { std::cout << "B()\n"; }
B( B const & ) { std::cout << "B( B const & )\n"; }
~B() { std::cout << "~B()\n"; }
B & operator=( B const & ) { std::cout << "B & operator=( B const
& )
\n"; return *this; }
template< typename T >
operator T() const;
};
//
class A
{
public:
A() { std::cout << "A()\n"; }
explicit A( int ) { std::cout << "A( int )\n"; }
A( A const & ) { std::cout << "A( A const & )\n"; }
~A() { std::cout << "~A()\n"; }
A & operator=( A const & ) { std::cout << "A & operator=( A const
& )
\n"; return *this; }
};
//
template< typename T >
B::operator T() const { std::cout << "B::operator T() const\n"; return
T(); }
//
int main( )
{
B obj001;
A obj002 = obj001; // this works

The standard mandates that this behaves as if a temporary A object is
created first from obj001 and then obj002 is copy constructed from this
temporary object (to further complicate things the actual copy
construction may be elided but the compiler must check that it would have
been possible). Creating the temporary object is an implicit conversion
and hence the explicit A(int) is not considered.
//A obj003( obj001 ); // this is ambiguous

This is an explicit constructor call hence A(int) as well as the copy
constructor are considered.
}

--
Markus Schoder- Hide quoted text -

- Show quoted text -
Thanks, I think I got it.
But what if I remove the explicit on the constructor.
Does the compiler choose the "shortest" implicit conversion?
This means it chooses:

1. B::operator T() const [ with T=A ]

instead of

1. B::operator T() const [ with T = int ]
and then
2. A( int ) // if I remove the explicit this could be used for
implicit conversion

It makes sense to choose the "shortest" path... I'm asking just to
make sure I got it.
Thanks again and sorry for the newbie questions
FB

Sep 3 '07 #5
xt*********@gmail.com schrieb:
This means it chooses:

1. B::operator T() const [ with T=A ]

instead of

1. B::operator T() const [ with T = int ]
and then
2. A( int ) // if I remove the explicit this could be used for
implicit conversion
How is the first onw shorter than the second? It is still ambiguous:

first case:
B::operator A ()
A(A const&)

second case:
B::operator int()
A(int)

Just because in the first case the intermediate value is already of type
"A" does not make the conversion shorter. A reasonable approach would be
to provide a A(B const&) constructor.
Frank
Sep 3 '07 #6
On 2007-09-03 11:42, xt*********@gmail.com wrote:
On Sep 3, 2:44 am, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
>On Sun, 02 Sep 2007 17:58:17 +0000, xtrigger303 wrote:
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 summarized it in the code
below. I was expecting both things at the end to work or not work at
all....
Any insight?
Thanks in advance,
Francesco
#include <iostream>
class A;
//
class B
{
public:
B() { std::cout << "B()\n"; }
B( B const & ) { std::cout << "B( B const & )\n"; }
~B() { std::cout << "~B()\n"; }
B & operator=( B const & ) { std::cout << "B & operator=( B const
& )
\n"; return *this; }
template< typename T >
operator T() const;
};
//
class A
{
public:
A() { std::cout << "A()\n"; }
explicit A( int ) { std::cout << "A( int )\n"; }
A( A const & ) { std::cout << "A( A const & )\n"; }
~A() { std::cout << "~A()\n"; }
A & operator=( A const & ) { std::cout << "A & operator=( A const
& )
\n"; return *this; }
};
//
template< typename T >
B::operator T() const { std::cout << "B::operator T() const\n"; return
T(); }
//
int main( )
{
B obj001;
A obj002 = obj001; // this works

The standard mandates that this behaves as if a temporary A object is
created first from obj001 and then obj002 is copy constructed from this
temporary object (to further complicate things the actual copy
construction may be elided but the compiler must check that it would have
been possible). Creating the temporary object is an implicit conversion
and hence the explicit A(int) is not considered.
//A obj003( obj001 ); // this is ambiguous

This is an explicit constructor call hence A(int) as well as the copy
constructor are considered.
}

--
Markus Schoder- Hide quoted text -

- Show quoted text -

Thanks, I think I got it.
But what if I remove the explicit on the constructor.
Does the compiler choose the "shortest" implicit conversion?
No, when writing 'A obj002 = obj001;' it means that the copy constructor
will be used (even though it might be elided), so the other constructors
do not matter.

--
Erik Wikström
Sep 3 '07 #7
On Sep 3, 12:25 pm, Frank Birbacher <bloodymir.c...@gmx.netwrote:
xtrigger...@gmail.com schrieb:
This means it chooses:
1. B::operator T() const [ with T=A ]
instead of
1. B::operator T() const [ with T = int ]
and then
2. A( int ) // if I remove the explicit this could be used for
implicit conversion

How is the first onw shorter than the second? It is still ambiguous:

first case:
B::operator A ()
A(A const&)

second case:
B::operator int()
A(int)

Just because in the first case the intermediate value is already of type
"A" does not make the conversion shorter. A reasonable approach would be
to provide a A(B const&) constructor.

Frank
Just to clarify: I'm working on a smart pointer library with different
templated smart pointers types ( like strong, weak, shared ) and a
central memory tracking system for debugging purposes. The example
above was just to show the problem... I'm actually working around the
problem but I was curious... Anyway the previously mentioned
simplified example is NOT ambiguous.

In fact
B obj1;
A obj2 = obj1;

compiles perfectly, this means that the compiler chooses one of the
two paths.
So either

1. B::operator T() [ with T = A ]
2. A( A const & ) // eliding copy constructor

or

1. B::operator T() [ with T = int ]
2. A( int ) // taking away explicit

Since the compiler chooses, I guess it chooses the first...
Thanks for the insights
FB

Sep 3 '07 #8
On Sep 2, 11:42 pm, xtrigger...@gmail.com wrote:
On Sep 2, 8:52 pm, Erik Wikström <Erik-wikst...@telia.comwrote:


On 2007-09-02 19:58, xtrigger...@gmail.com wrote:
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 summarized it in the code
below. I was expecting both things at the end to work or not work at
all....
Any insight?
Thanks in advance,
Francesco
#include <iostream>
class A;
//
class B
{
public:
B() { std::cout << "B()\n"; }
B( B const & ) { std::cout << "B( B const & )\n"; }
~B() { std::cout << "~B()\n"; }
B & operator=( B const & ) { std::cout << "B & operator=( B const & )
\n"; return *this; }
template< typename T >
operator T() const;
};
//
class A
{
public:
A() { std::cout << "A()\n"; }
explicit A( int ) { std::cout << "A( int )\n"; }
A( A const & ) { std::cout << "A( A const & )\n"; }
~A() { std::cout << "~A()\n"; }
A & operator=( A const & ) { std::cout << "A & operator=( A const & )
\n"; return *this; }
};
//
template< typename T >
B::operator T() const { std::cout << "B::operator T() const\n";
return T(); }
//
int main( )
{
B obj001;
A obj002 = obj001; // this works
//A obj003( obj001 ); // this is ambiguous
}
Since B can be converted to any type you like, it can be converted to
either A or int. Both of which are types for which A has a constructor,
and the compiler have no idea which of them you would like to use. Using
explicit on a constructor only prevents it from being used like this:
A a = 1;
You have to do
A a(1);
--
Erik Wikström

In fact I was wondering why the first initialization works ( it is not
ambiguous it seems...)
A obj002 = obj001; // this works

while the second doesn't.
//A obj003( obj001 ); // this is ambiguous

I figured that the explicit doesn't help here.
:-)- Hide quoted text -

- Show quoted text -
explicit causes the first not to be ambiguous.do:
template<typename T>
T& B::To();

A obj003(obj001.To<A>());

regards,
FM.

Sep 3 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jason Heyes | last post by:
I have a class with two constructors that both take the same type of argument as a parameter. What should I do to disambiguate my class?
7
by: ishekara | last post by:
Hi, I am having a class template which is used to convert from one type another. I am having a problem when i use the copy constructor with same type. code. #include "stdio.h" template...
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;
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...
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
6
by: c1t1z3n | last post by:
hiya, i'm having this weird error on my project. As far as I know "ambiguous call to overloaded function" should only occur when the compiler must choose from several methods, but here i don't think...
4
by: Joseph Turian | last post by:
I have a templated class with the following methods: Vocab(const T& t); Vocab(unsigned uid); However, when T = unsigned, and I call Vocab(unsigned(0)) then the compiler rightly complains about...
32
by: Anna Smidt | last post by:
I am having an "ambiguous call to overloaded function" error again. This is the function: int nGetProfWidth (int ncols, unsigned ProfSpec) { if ((ProfSpec & PROF_2d) == 0) return ncols;...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.