D
dru
Every time I think I know or understand C++ with some
confidence, I see something that changes that.
I was looking at the boost shared_ptr docs and code, and
I ran across the operator unspecified_bool_type()
In the code they have this:
#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
operator bool () const
{
return px != 0;
}
#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__,
BOOST_TESTED_AT(0x3003))
typedef T * (this_type::*unspecified_bool_type)() const;
operator unspecified_bool_type() const // never throws
{
return px == 0? 0: &this_type::get;
}
#else
typedef T * this_type::*unspecified_bool_type;
operator unspecified_bool_type() const // never throws
{
return px == 0? 0: &this_type:x;
}
#endif
// operator! is redundant, but some compilers need it
bool operator! () const // never throws
{
return px == 0;
}
Here are some things to note.
1. That SunCC implementation is very easy to understand, and is often
used in MSVC as well.
2. The MWERKS workaround is fairly weird. Skip it.
3. The third section is the real code. What is the operator really of
with that typdef that I cannot parse???
4. I included the operator ! just for fun. That implementation appears
to be
straigthfowrard for all implementations. It's simplicity is probably a
clue why the other ones are not; some asymetry in the C++ semantics.
*Please post replies here, thx*
confidence, I see something that changes that.
I was looking at the boost shared_ptr docs and code, and
I ran across the operator unspecified_bool_type()
In the code they have this:
#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
operator bool () const
{
return px != 0;
}
#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__,
BOOST_TESTED_AT(0x3003))
typedef T * (this_type::*unspecified_bool_type)() const;
operator unspecified_bool_type() const // never throws
{
return px == 0? 0: &this_type::get;
}
#else
typedef T * this_type::*unspecified_bool_type;
operator unspecified_bool_type() const // never throws
{
return px == 0? 0: &this_type:x;
}
#endif
// operator! is redundant, but some compilers need it
bool operator! () const // never throws
{
return px == 0;
}
Here are some things to note.
1. That SunCC implementation is very easy to understand, and is often
used in MSVC as well.
2. The MWERKS workaround is fairly weird. Skip it.
3. The third section is the real code. What is the operator really of
with that typdef that I cannot parse???
4. I included the operator ! just for fun. That implementation appears
to be
straigthfowrard for all implementations. It's simplicity is probably a
clue why the other ones are not; some asymetry in the C++ semantics.
*Please post replies here, thx*