Topic: bool ambiguity
Author: Tim Ottinger <ottinger@oma.com>
Date: 1998/03/01 Raw View
Manfred Knemeyer wrote:
> bool is a reserved keyword (type)
> - it is not an operator.
Come, now, we can use all types as cast operators. This (slightly modified)
version generates no warnings at all:
class X
{
public:
operator bool() { return u != 0; } // I added parenthesis and
'return'
operator unsigned() { return u; } // I added parenthesis
private:
unsigned u; // I just like private stuff coming last...
};
int function()
{
X x;
if (x) return 1;
return 0;
}
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/02/20 Raw View
If I have a class with an operator bool and an operator unsigned and use
a statement such as "if (x)" where the type of x is the class I have
just described, my compiler complains that this is ambiguous between the
two operators. For example
class X
{
unsigned u;
public:
operator bool { u != 0; }
operator unsigned { return u; }
};
X x;
if (x)
{ etc. }
Is my compiler behind the times in implementing the standard or am I
using the syntax incorrectly ?
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Manfred Knemeyer" <nospam@ix.netcom.com>
Date: 1998/02/20 Raw View
bool is a reserved keyword (type)
- it is not an operator.
Edward Diener wrote in message <34ECEAAE.6C1CC981@abraxis.com>...
>If I have a class with an operator bool and an operator unsigned and use
>a statement such as "if (x)" where the type of x is the class I have
>just described, my compiler complains that this is ambiguous between the
>two operators. For example
>
>class X
> {
> unsigned u;
> public:
> operator bool { u != 0; }
> operator unsigned { return u; }
> };
>
>X x;
>
>if (x)
> { etc. }
>
>Is my compiler behind the times in implementing the standard or am I
>using the syntax incorrectly ?
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/02/21 Raw View
The misprint is my mistake. I meant to write:
class X
{
unsigned u;
public:
operator bool() { u != 0; }
operator unsigned() { return u; }
};
I must have been half asleep or brain dead when I posted my example.
Manfred Knemeyer wrote:
> bool is a reserved keyword (type)
> - it is not an operator.
>
> Edward Diener wrote in message <34ECEAAE.6C1CC981@abraxis.com>...
> >If I have a class with an operator bool and an operator unsigned and use
> >a statement such as "if (x)" where the type of x is the class I have
> >just described, my compiler complains that this is ambiguous between the
> >two operators. For example
> >
> >class X
> > {
> > unsigned u;
> > public:
> > operator bool { u != 0; }
> > operator unsigned { return u; }
> > };
> >
> >X x;
> >
> >if (x)
> > { etc. }
> >
> >Is my compiler behind the times in implementing the standard or am I
> >using the syntax incorrectly ?
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/02/21 Raw View
Manfred Knemeyer writes:
> bool is a reserved keyword (type)
> - it is not an operator.
That's exactly why one can define converter member functions, named
after the type name. If one declares `operator bool()' inside a
class, it means an instance of that class can be implicitly converted
to type bool by using this member function.
>> my compiler complains that this is ambiguous between the
>> two operators
>> operator bool { u != 0; }
>> operator unsigned { return u; }
There must be parentheses after the type name:
operator bool () { /*... */ }
>> if (x)
This should use `operator bool()' to convert object `x' to type bool.
If your compiler complains it is ambiguous, then it is broken.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Paul Sorensen" <psorensen@bigpond.com>
Date: 1998/02/21 Raw View
I have a similar problem when I'm using the IBM Visual Age compiler when
I've got conversion operators for both integers and doubles. I've always
assumed that it's just because the compiler is behind the times.
By the way, your bool() operator should read { return u != 0; }.
Edward Diener wrote in message <34EE421F.4750F51B@abraxis.com>...
>The misprint is my mistake. I meant to write:
>
>class X
> {
> unsigned u;
> public:
> operator bool() { u != 0; }
> operator unsigned() { return u; }
> };
>
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]