Topic: Why in the world is this ambiguous?
Author: MattDelB@nospam.com ("MiniDisc_2k2")
Date: Tue, 8 Jul 2003 04:23:47 +0000 (UTC) Raw View
"Davis King" <davisking@mail.com> wrote in message
news:3F08C970.5A0FEDF8@mail.com...
>
> The following code does not compile on any compiler I have access to
> (gcc, borland, visual studio). The error I get is that
> it can't convert from an int to aaa. But why in the world is this
> ambiguous? It's private! Is this a compiler error or is this the
> correct behavior? And if so why?
>
> Making the private constructor explicit makes it compile but its not
> like a private constructor can be used in this instance in the first
> place.
>
> class aaa
> {
> aaa(int*) { }
> public:
> aaa(unsigned short a) {}
> };
>
>
> int main()
> {
> aaa testmeme = 0;
> }
>
>
> -Davis
>
It makes no difference whether or not it COULD be called, it matters if it
would be possible that that function would meet the definition. The standard
states that the compiler must determine
exactly which function to use, and THEN determine if it is legal to be
called. This actually makes more sense than having the compiler look at all
aspects at once. Doing that would probably result in just more programmers
having errors they can't find.
--
MiniDisc_2k2
To reply, replace nospam.com with cox dot net.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: rogero@howzatt.demon.co.uk ("Roger Orr")
Date: Thu, 10 Jul 2003 03:53:14 +0000 (UTC) Raw View
"Davis King" <davisking@mail.com> wrote in message
news:3F08C970.5A0FEDF8@mail.com...
>
> The following code does not compile on any compiler I have access to
> (gcc, borland, visual studio). The error I get is that
> it can't convert from an int to aaa. But why in the world is this
> ambiguous? It's private! Is this a compiler error or is this the
> correct behavior? And if so why?
As others have said, C++ tries to find the correct function first, then sees
if you have access.
> Making the private constructor explicit makes it compile but its not
> like a private constructor can be used in this instance in the first
> place.
Making the constructor explicit only works because your code uses
initialisation with '='.
The statement
aaa testmeme = 0;
is valid only if a temporary aaa object can be constructed from 0, and
making a ctor explicit removes it from the possible set of ctors.
[Almost all compilers will optimise away this temporary -- and are
explicitly allowed to -- but the code is only valid if the temporary could
have been constructed]
However, if you (or other users of your class!) use direct initialisation:
aaa testmeme( 0 );
then the code remains ambiguous even when you add explicit to (either)
constructor.
You can resolve the ambiguity by specifying the exact data type:
aaa testmeme( (int*)0 );
but this is a bit messy.
Why do you need both constructors? Perhaps there's another solution to the
problem.
Regards,
Roger Orr
--
MVP in C++ at www.brainbench.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: davisking@mail.com (Davis King)
Date: Mon, 7 Jul 2003 03:49:33 +0000 (UTC) Raw View
The following code does not compile on any compiler I have access to
(gcc, borland, visual studio). The error I get is that
it can't convert from an int to aaa. But why in the world is this
ambiguous? It's private! Is this a compiler error or is this the
correct behavior? And if so why?
Making the private constructor explicit makes it compile but its not
like a private constructor can be used in this instance in the first
place.
class aaa
{
aaa(int*) { }
public:
aaa(unsigned short a) {}
};
int main()
{
aaa testmeme = 0;
}
-Davis
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: john_andronicus@hotmail.com ("John Harrison")
Date: Mon, 7 Jul 2003 04:46:53 +0000 (UTC) Raw View
"Davis King" <davisking@mail.com> wrote in message
news:3F08C970.5A0FEDF8@mail.com...
>
> The following code does not compile on any compiler I have access to
> (gcc, borland, visual studio). The error I get is that
> it can't convert from an int to aaa. But why in the world is this
> ambiguous? It's private! Is this a compiler error or is this the
> correct behavior? And if so why?
>
> Making the private constructor explicit makes it compile but its not
> like a private constructor can be used in this instance in the first
> place.
>
> class aaa
> {
> aaa(int*) { }
> public:
> aaa(unsigned short a) {}
> };
>
>
> int main()
> {
> aaa testmeme = 0;
> }
>
>
> -Davis
>
Simple. Compiler works out which function to call *before* examining whether
said functions are public or private.
This is as per the standard.
john
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]