Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > T::operator int () const ambiguous with T::operator Handle () const?

Reply
Thread Tools

T::operator int () const ambiguous with T::operator Handle () const?

 
 
Tim Clacy
Guest
Posts: n/a
 
      04-29-2005
Please illuminate; what operator of class 'Event' will get matched for these
two cases [see Event outline later]:

Event ev1;
Event ev2;

// Case 1
//
if (ev1)
;

// Case 2
//
if (ev1 || ev2)
;

I would have thought 'operator int ()' is the only obvious match, but my
compiler generates errors:

"ambiguous 3-way choice of conversion from 'struct Event' in Boolean
context

All I'm trying to do is wrap an OS event in a class that can be treated like
a Boolean; can anyone explain what the ambiguity is and how it can be
resolved (preferable without casts)?

Best regards


Tim





typedef struct tagHandle { }* Handle;

bool eventIsSignalled(Handle hEvent);

struct Event
{
:
bool IsSignalled() const { return eventIsSignalled(*this); }

operator int () const { return IsSignalled(); }
operator Handle () const { return handle; }

private:
Handle handle;
};


 
Reply With Quote
 
 
 
 
Shezan Baig
Guest
Posts: n/a
 
      04-29-2005

Tim Clacy wrote:
> typedef struct tagHandle { }* Handle;
>
> bool eventIsSignalled(Handle hEvent);
>
> struct Event
> {
> :
> bool IsSignalled() const { return eventIsSignalled(*this); }
>
> operator int () const { return IsSignalled(); }
> operator Handle () const { return handle; }




ints *and* pointers are convertible to boolean (with the same
priority). So it is ambigious.



>
> private:
> Handle handle;
> };


 
Reply With Quote
 
 
 
 
abecedarian@spambob.com
Guest
Posts: n/a
 
      04-29-2005
Shezan Baig wrote:
> Tim Clacy wrote:
> > typedef struct tagHandle { }* Handle;


try instead:
struct Handle { SomeType* realHandle; };

> > bool eventIsSignalled(Handle hEvent);
> >
> > struct Event
> > {
> > :
> > bool IsSignalled() const { return eventIsSignalled(*this); }
> >
> > operator int () const { return IsSignalled(); }
> > operator Handle () const { return handle; }

>
> ints *and* pointers are convertible to boolean (with the same
> priority). So it is ambigious.


 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      04-30-2005

> All I'm trying to do is wrap an OS event in a class that can be treated

like
> a Boolean; can anyone explain what the ambiguity is and how it can be
> resolved (preferable without casts)?
> ...
> typedef struct tagHandle { }* Handle;
>
> bool eventIsSignalled(Handle hEvent);
>
> struct Event
> {
> :
> bool IsSignalled() const { return eventIsSignalled(*this); }
>
> operator int () const { return IsSignalled(); }
> operator Handle () const { return handle; }
>
> private:
> Handle handle;
> };
>


If Event::IsSignalled does fine, why use operator int?
Event itself is not a handle, why provide Handle operator?

To simplify:

struct Event
{
private:
Handle handle;

public:
Handle GetHandle() const
{
return handle;
}

operator int() const
{
return eventIsSignalled();
}

//...
};


 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      04-30-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Shezan Baig wrote:
> > Tim Clacy wrote:
> > > typedef struct tagHandle { }* Handle;

>
> try instead:
> struct Handle { SomeType* realHandle; };


Shezen,

Hi. I can't try right now but how would that help?

> ints *and* pointers are convertible to boolean (with the same
> priority). So it is ambigious.


Hmm, that means that no C++ class can be used in an 'if' statement if it has
more than one operator that returns a POD type... doesn't it?


 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      04-30-2005

"ben" <(E-Mail Removed)> wrote in message
news:42733154$0$4656$(E-Mail Removed) ...
>
> > All I'm trying to do is wrap an OS event in a class that can be treated

> like
> > a Boolean; can anyone explain what the ambiguity is and how it can be
> > resolved (preferable without casts)?
> > ...
> > typedef struct tagHandle { }* Handle;
> >
> > bool eventIsSignalled(Handle hEvent);
> >
> > struct Event
> > {
> > :
> > bool IsSignalled() const { return eventIsSignalled(*this); }
> >
> > operator int () const { return IsSignalled(); }
> > operator Handle () const { return handle; }
> >
> > private:
> > Handle handle;
> > };
> >

>
> If Event::IsSignalled does fine, why use operator int?
> Event itself is not a handle, why provide Handle operator?
>
> To simplify:
>
> struct Event
> {
> private:
> Handle handle;
>
> public:
> Handle GetHandle() const
> {
> return handle;
> }
>
> operator int() const
> {
> return eventIsSignalled();
> }
>
> //...
> };


Ben,

Hi. It looks like I'll have to run with your suggestion. 'operator int'; is
convenient in 'if' and || expressions for exactly the same reason that you
would prefer this:

if (a || b || c)
;

to this:

if ((a != false) || (b != false) || (c != false))
;

I'm a little disappointed that 'operator in' isn't a better match that
'operator void*' in an 'if' expression. This is surely a case of 'C'
crippling the power of C++ operator overloading isn't it?

Regards


Tim


 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      04-30-2005
Tim wrote:

>> ints *and* pointers are convertible to boolean (with the same
>> priority). So it is ambigious.

> Hmm, that means that no C++ class can be used in an 'if' statement if it
> has more than one operator that returns a POD type... doesn't it?


Just define a conversion to bool for that class.

--
Salu2
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      04-30-2005
Tim wrote:
>
> Hmm, that means that no C++ class can be used in an 'if' statement if it has
> more than one operator that returns a POD type... doesn't it?
>


No. POD types aren't inherently convertible to bool. Some of them (in
particular, builtin types) are.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      05-01-2005
Julián Albo wrote:
> Tim wrote:
>
>
>>>ints *and* pointers are convertible to boolean (with the same
>>>priority). So it is ambigious.

>>
>>Hmm, that means that no C++ class can be used in an 'if' statement if it
>>has more than one operator that returns a POD type... doesn't it?

>
>
> Just define a conversion to bool for that class.
>

And then live with all the bizarre run time errors such an operator will
give you!

Use a bool member like isOK(), the extra typing is worth it.

Ian
 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      05-01-2005
Ian wrote:

> Julián Albo wrote:
>> Tim wrote:
>>
>>
>>>>ints *and* pointers are convertible to boolean (with the same
>>>>priority). So it is ambigious.
>>>
>>>Hmm, that means that no C++ class can be used in an 'if' statement if it
>>>has more than one operator that returns a POD type... doesn't it?

>> Just define a conversion to bool for that class.

> And then live with all the bizarre run time errors such an operator will
> give you!
> Use a bool member like isOK(), the extra typing is worth it.


I do. But the question was if that can be done.

--
Salu2
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
const int versus #define - does "const int" take up space in exe? ThazKool C++ 1 06-11-2006 04:55 AM
unsigned int const does not match const unsigned int Timo Freiberger C++ 3 10-30-2004 07:02 PM



Advertisments