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::operato r T() const\n";
return T(); }
//
int main( )
{
B obj001;
A obj002 = obj001; // this works
//A obj003( obj001 ); // this is ambiguous
} 8 3103
On 2007-09-02 19:58, xt*********@gma il.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::operato r 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
On Sep 2, 8:52 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-02 19:58, xtrigger...@gma il.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::operato r 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.
:-)
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::operato r 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
On Sep 3, 2:44 am, Markus Schoder <a3vr6dsg-use...@yahoo.de wrote:
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::operato r 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 xt*********@gma il.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
On 2007-09-03 11:42, xt*********@gma il.com wrote:
On Sep 3, 2:44 am, Markus Schoder <a3vr6dsg-use...@yahoo.de wrote:
>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::operato r 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
On Sep 3, 12:25 pm, Frank Birbacher <bloodymir.c... @gmx.netwrote:
xtrigger...@gma il.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
On Sep 2, 11:42 pm, xtrigger...@gma il.com wrote:
On Sep 2, 8:52 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-02 19:58, xtrigger...@gma il.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::operato r 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<typena me T>
T& B::To();
A obj003(obj001.T o<A>());
regards,
FM. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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 <class T>
class SPtr
{
|
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;
|
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 mtd::mVector&,
mtd::mBCTYPE) const'
are ambiguous even though the worst conversion for the former is better
than the worst conversion for the latter
|
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 A, and the
second one of type B. Let say, these are not my classes and I know
nothing about the implementation. As system doesn't know what code
must be used for resolving the language construction a+b (where A a;
and B b;), it returns "The call...
| |
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
internally relies on the at function of the superclass.
#include <vector>
|
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 it's the case. I've got the following class:
class item
{
public:
item(string id_,string datatype_);
//item(string id_,string datatype_,int periodo_);
//item(string id_,string datatype_,string headerfile_);
//item(string id_, string...
|
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 an ambiguous call to the overloaded
function.
How can I specify which method I want called in this case?
Thanks,
|
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;
return int(sqrt(ncols)); //HERE THE ERROR IS THROWN
}
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |