Enum |= and |

T

thetrueaplus

How do I allow my bitshifted enum to work with |= and |?

facebook::Gender::Gender facebook::Gender::fromXml(const QDomElement
&val)
{
if (val.nodeName() == "gender")
return facebook::Gender::fromString(val.text());
if (val.nodeName() == "meeting_sex")
{
Gender retval = facebook::Gender::unspecified;
QDomElement elt = val.firstChildElement();
while (!elt.isNull())
{
if (elt.nodeName() == "meeting_sex_elt")
retval |= fromString(elt.text()); //*********Error here (invalid
conversion from int to facebook::Gender::Gender)
elt.nextSiblingElement();
}
return retval;
}
return facebook::Gender::unspecified;
}
 
J

John Grabner

How do I allow my bitshifted enum to work with |= and |?

facebook::Gender::Gender facebook::Gender::fromXml(const QDomElement
&val)
{
if (val.nodeName() == "gender")
return facebook::Gender::fromString(val.text());
if (val.nodeName() == "meeting_sex")
{
Gender retval = facebook::Gender::unspecified;
QDomElement elt = val.firstChildElement();
while (!elt.isNull())
{
if (elt.nodeName() == "meeting_sex_elt")
retval |= fromString(elt.text()); //*********Error here (invalid
conversion from int to facebook::Gender::Gender)
elt.nextSiblingElement();
}
return retval;
}
return facebook::Gender::unspecified;
}
Gender &operator |=(Gender &lhs, const Gender &rhs)
{
return lhs = lhs | rhs;
}

This should get you started.

John.
 
T

thetrueaplus

John said:
Gender &operator |=(Gender &lhs, const Gender &rhs)
{
return lhs = lhs | rhs;
}

This should get you started.

John.

That still errors on this line:
return lhs = lhs | rhs;
 
K

Kai-Uwe Bux

John said:
Gender &operator |=(Gender &lhs, const Gender &rhs)
{
return lhs = lhs | rhs;

I think, that would not compile (and Comeau is with me): to compute

lhs | rhs

the enums get converted to their underlying integral type. The result has
not enum-type. You need to cast it back:

return ( lhs = Gender( lhs | rhs ) );

Of course, the cast also shows that one should think twice before doing
this.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top