 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
New_user Guest
|
Posted: Fri Apr 16, 2004 10:10 am Post subject: Overloading resolution |
|
|
Hello.
Two fragments:
//Fragment 1, no ambiquity
class A
{
public:
A & operator = (const char *);
};
class B
{
public:
operator A &();
operator char *();
};
void g()
{
B b;
A a;
a = b;
}
//Fragment 2 ambiquity !!!
class A
{
public:
A & operator = (char);
};
class B
{
public:
operator A &();
operator float();
};
void g()
{
B b;
A a;
a = b;
}
Code was compiled with Comeau online test. Please, explain me, WHAT'S
THE DIFFERENCE between fragment1 and fragment2 ??? What's the
overloading rules in this cases?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mikael Kilpeläinen Guest
|
Posted: Sat Apr 17, 2004 6:12 am Post subject: Re: Overloading resolution |
|
|
New_user wrote:
| Quote: | Hello.
Two fragments:
//Fragment 1, no ambiquity
class A
{
public:
A & operator = (const char *);
};
class B
{
public:
operator A &();
operator char *();
};
void g()
{
B b;
A a;
a = b;
}
|
I think this is ambiguous also. If you make the conversion function to
return a pointer to const char, comeau thinks it is ambiguous. This seems
somewhat strange since the second standard conversion function of the
user-defined conversion sequence should not be considered because the
destination is not same and different user-defined conversions are used.
There are two viable functions:
A& operator=(char const*);
A& operator=(A const&);
and hence two conversion sequences exist, these sequences have a different
destination parameter and they use different user-defined conversion and so
it is ambiguous. btw, Comeau 4.3.0.1 thinks it is ambiguous but the newer
versions does not.
Mikael
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Max Zinal Guest
|
Posted: Sat Apr 17, 2004 6:34 am Post subject: Re: Overloading resolution |
|
|
Although I am not a guru in the Holy Standard, IMHO the situation is
simple enough. In the first case the two possible choices are
between implicit "operator=(const A&)" and explicit
"operator = (const char *)". "char*" to "const char*" conversion
is chosen, because it is
(a) explicit
(b) does not require additional assumptions from the compiler -
char* - to const char* - conversion only appends constness
to type - a semantically "safe" operation.
In the second case you want the compiler to choose between
"operator=(const A&)" and "operator=(char)", and the argument
is float. char and float are different types, so the compiler
really needs more info to perform that choice. C++ is a tricky
language in some of its aspects.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
New_user Guest
|
Posted: Sun Apr 18, 2004 12:41 am Post subject: Re: Overloading resolution |
|
|
btw, Comeau 4.3.0.1 thinks it is ambiguous but the newer
| Quote: | versions does not.
|
Yes, right. I think, it's the common problem of compilers based on new
EDG front-end - I tried : Borland C++ Technical Preview, Intel C++
compiler for windows 8.0, Intel C++ compiler for Linux 7.1.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Mang Guest
|
Posted: Sun Apr 18, 2004 12:42 am Post subject: Re: Overloading resolution |
|
|
Mikael Kilpeläinen schrieb:
| Quote: | New_user wrote:
Hello.
Two fragments:
//Fragment 1, no ambiquity
class A
{
public:
A & operator = (const char *);
};
class B
{
public:
operator A &();
operator char *();
};
void g()
{
B b;
A a;
a = b;
}
I think this is ambiguous also. If you make the conversion function to
return a pointer to const char, comeau thinks it is ambiguous. This seems
somewhat strange since the second standard conversion function of the
user-defined conversion sequence should not be considered because the
destination is not same and different user-defined conversions are used.
There are two viable functions:
A& operator=(char const*);
A& operator=(A const&);
|
What I don't understand here is:
Why does the compiler still create an implicit assignment operator of type
A& operator=(A const&)?
Doesn't the explicit declaration of the other assignment operator (A&
operator=(char const*)) suppress the implicit creation of the assignment
operator?
(Like, if I declare a non-default constructor, this one suppresses the creation
of a compiler-generated default constructor).
Which part of the Standard covers this?
regards,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sun Apr 18, 2004 2:51 pm Post subject: Re: Overloading resolution |
|
|
In message <4081378F.33C11D9D (AT) unet (DOT) univie.ac.at>, Thomas Mang
<a9804814 (AT) unet (DOT) univie.ac.at> writes
| Quote: | What I don't understand here is:
Why does the compiler still create an implicit assignment operator of type
A& operator=(A const&)?
Doesn't the explicit declaration of the other assignment operator (A&
operator=(char const*)) suppress the implicit creation of the assignment
operator?
(Like, if I declare a non-default constructor, this one suppresses the creation
of a compiler-generated default constructor).
|
However if you do not declare one of the four possible copy ctors (&,
const&, volatile&, const volatile&) the compiler will generate a copy
ctor with a const & parameter. The same rule applies to copy assignment
operator. IOWs the copy semantics is always provided (if possible)
unless you explicitly suppress it.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mikael Kilpeläinen Guest
|
Posted: Sun Apr 18, 2004 2:58 pm Post subject: Re: Overloading resolution |
|
|
Thomas Mang wrote:
| Quote: |
A& operator=(char const*);
A& operator=(A const&);
What I don't understand here is:
Why does the compiler still create an implicit assignment operator of type
A& operator=(A const&)?
|
Because A& operator=(char const*) is not a copy assignment operator, but an
assignment operator. User-defined copy assignment operator takes one of the
following parameters: X, X&, const X&, volatile X& or const volatile X&.
If no user-defined copy assignment operator is declared, one is declared
implicitly. see std03 12.8/9 and 12.8/10
| Quote: | (Like, if I declare a non-default constructor, this one suppresses the
creation of a compiler-generated default constructor).
|
Yes, this is different case.
Mikael
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Mon Apr 19, 2004 5:39 am Post subject: Re: Overloading resolution |
|
|
On 17 Apr 2004 20:42:59 -0400, Thomas Mang <a9804814 (AT) unet (DOT) univie.ac.at>
wrote:
| Quote: | Mikael Kilpeläinen schrieb:
New_user wrote:
class A
{
public:
A & operator = (const char *);
};
class B
{
public:
operator A &();
operator char *();
};
void g()
{
B b;
A a;
a = b;
}
I think this is ambiguous also. If you make the conversion function to
return a pointer to const char, comeau thinks it is ambiguous. This seems
somewhat strange since the second standard conversion function of the
user-defined conversion sequence should not be considered because the
destination is not same and different user-defined conversions are used.
There are two viable functions:
A& operator=(char const*);
A& operator=(A const&);
What I don't understand here is:
Why does the compiler still create an implicit assignment operator of type
A& operator=(A const&)?
|
Because every class must declare a copy assignment operator. The
possible parameters are A, A&, A const&, A volatile&, A const volatile&.
| Quote: | Doesn't the explicit declaration of the other assignment operator (A&
operator=(char const*)) suppress the implicit creation of the assignment
operator?
|
No.
| Quote: | (Like, if I declare a non-default constructor, this one suppresses the creation
of a compiler-generated default constructor).
|
Yes, but the same is not true for the copy constructor. Every class
will declare at least one of the four possible copy constructors 12.8/4
and one of the five possible copy assignment operators 12.8/10.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Apr 19, 2004 10:01 pm Post subject: Re: Overloading resolution |
|
|
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | In message <4081378F.33C11D9D (AT) unet (DOT) univie.ac.at>, Thomas Mang
[email]a9804814 (AT) unet (DOT) univie.ac.at[/email]> writes
What I don't understand here is:
Why does the compiler still create an implicit assignment operator of
type A& operator=(A const&)?
Doesn't the explicit declaration of the other assignment operator (A&
operator=(char const*)) suppress the implicit creation of the
assignment operator?
(Like, if I declare a non-default constructor, this one suppresses
the creation of a compiler-generated default constructor).
However if you do not declare one of the four possible copy ctors (&,
const&, volatile&, const volatile&) the compiler will generate a copy
ctor with a const & parameter. The same rule applies to copy
assignment operator. IOWs the copy semantics is always provided (if
possible) unless you explicitly suppress it.
|
The real question here is how to suppress it without providing a
signature which will be considered for overload resolution. The usual
technique of declaring it private will not work, because access isn't
considered until overload resolution has finished.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Tue Apr 20, 2004 7:19 pm Post subject: Re: Overloading resolution |
|
|
Francis Glassborow wrote:
| Quote: | However if you do not declare one of the four possible copy ctors (&,
const&, volatile&, const volatile&) the compiler will generate a copy
ctor with a const & parameter. The same rule applies to copy
assignment operator. IOWs the copy semantics is always provided (if
possible) unless you explicitly suppress it.
|
There are cases however when a compiler has to generate copy ctor with
non-const & parameter.
Like here:
struct some
{
some() : p(0) {}
std::auto_ptr<int> p;
};
int main()
{
some a;
some b(a);
// won't compile when uncommented
//some const c;
//some d(c);
}
--
Maxim Yegorushkin
MetaCommunications Engineering
http://www.meta-comm.com/engineering/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Annamalai Gurusami Guest
|
Posted: Tue Apr 20, 2004 10:27 pm Post subject: Re: Overloading resolution |
|
|
Max Zinal <Zlat0 (AT) mail (DOT) ru> wrote
| Quote: | Although I am not a guru in the Holy Standard, IMHO the situation is
simple enough. In the first case the two possible choices are
between implicit "operator=(const A&)" and explicit
"operator = (const char *)". "char*" to "const char*" conversion
is chosen, because it is
|
But why does the presence of 'operator float()' in B cause the problem?
For example, the following piece of code compiles in g++ 3.2
#include <iostream>
class A
{
public:
A& operator = (char) { std::cout << "assign opern"; }
};
class B
{
public:
operator A &() { std::cout << "user-def B->An"; }
// operator float() { std::cout << "user-def B->floatn"; } // Line A
};
int main()
{
A a;
B b;
a = b;
}
But if I uncomment the 'Line A' then I get the following error
$ g++ test03.cxx
test03.cxx: In function `int main()':
test03.cxx:21: ambiguous overload for `A& = B&' operator
test03.cxx:7: candidates are: A& A::operator=(char)
test03.cxx:5: A& A::operator=(const A&)
Can anyone explain it further?
Rgds,
anna
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|