473,473 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is boost's unspecified_bool_type?

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::px;
}

#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*
Jul 22 '05 #1
8 10745

"dru" <dr******@redwoodsoft.com> wrote in message
news:6c**************************@posting.google.c om...
Every time I think I know or understand C++ with some
confidence, I see something that changes that.

I know that feeling.
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::px;
}

#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*


The unspecified_bool_type is a pointer to a member function. The problem
with the SunCC implementation is that operator bool could be used in places
you don't want. For instance with operator bool

shared_ptr<X> xp;
float f = xp;

becomes legal code (implicit call of operator bool, followed by promotion of
bool to float).

Pointers to member functions are used instead because they are much less
likely to be result in unintended conversions to other types, but are still
usable in situations where a Boolean value is needed

shared_ptr<X> xp;
if (xp) // implicit conversion to unspecified_bool_type, followed by
check for NULL
{
}

operator void* is also sometimes used for similar reasons, but operator
unspecified_bool_type is better I think.

john
Jul 22 '05 #2
dru
First, thanks for your reply.

The unspecified_bool_type is a pointer to a member function. The problem
with the SunCC implementation is that operator bool could be used in places
you don't want. For instance with operator bool

shared_ptr<X> xp;
float f = xp;

becomes legal code (implicit call of operator bool, followed by promotion of
bool to float).
I can see that, but I don't see a major threat there.

Pointers to member functions are used instead because they are much less
likely to be result in unintended conversions to other types, but are still
usable in situations where a Boolean value is needed

shared_ptr<X> xp;
if (xp) // implicit conversion to unspecified_bool_type, followed by
check for NULL
{
}

operator void* is also sometimes used for similar reasons, but operator
unspecified_bool_type is better I think.


Where can I find in the ARM the description of a conversion from a pointer
to a bool?
On a side note, I wish you could do the following with a shared pointer.

xyz = NULL;

It would be nice, but I understand the reasons for it's absence.
Jul 22 '05 #3

"dru" <dr******@redwoodsoft.com> wrote in message
news:6c**************************@posting.google.c om...
First, thanks for your reply.

The unspecified_bool_type is a pointer to a member function. The problem
with the SunCC implementation is that operator bool could be used in places you don't want. For instance with operator bool

shared_ptr<X> xp;
float f = xp;

becomes legal code (implicit call of operator bool, followed by promotion of bool to float).
I can see that, but I don't see a major threat there.


Not sure I agree with that. Unexpected converion to a numeric type can be
very confusing, usually easily fixed when spotted though.

[snip]

Where can I find in the ARM the description of a conversion from a pointer
to a bool?


Sorry I'm not familar with ARM. In the C++ standard it's section 4.12. All
it says is that any pointer or pointer to member can be converted to a bool
rvalue, and that null pointers convert to false and other values to true.

john
Jul 22 '05 #4
dru
Mr. Harrison,

Thanks for your help with this. This has been a great
help.

Dru
Jul 22 '05 #5
dru,

"dru" <dr******@redwoodsoft.com> wrote in message
news:6c**************************@posting.google.c om...
First, thanks for your reply.
[spip]
On a side note, I wish you could do the following with a shared pointer.

xyz = NULL;

It would be nice, but I understand the reasons for it's absence.


how about:

xyz.reset();

Jeff F
Jul 22 '05 #6
dru
> > On a side note, I wish you could do the following with a shared pointer.

xyz = NULL;

It would be nice, but I understand the reasons for it's absence.


how about:

xyz.reset();


Yep, know all about that. I would prefer = NULL to be like the
ole' MS style or .clear() to be like the STL folk.

dru
Jul 22 '05 #7
In article <6c**************************@posting.google.com >,
dr******@redwoodsoft.com (dru) wrote:
On a side note, I wish you could do the following with a shared pointer.

xyz = NULL;

It would be nice, but I understand the reasons for it's absence.


Actually I think that's a very good suggestion. I think it can be done
safely. We need a non-explicit shared_ptr constructor taking a pointer
to a private type (such as unspecified_bool_type). Hmm... I need to
give that a little more thought, but it looks promising to me.

-Howard
Jul 22 '05 #8
In message <6c**************************@posting.google.com >, dru
<dr******@redwoodsoft.com> writes
> On a side note, I wish you could do the following with a shared pointer.
>
> xyz = NULL;
>
> It would be nice, but I understand the reasons for it's absence.
how about:

xyz.reset();


Yep, know all about that. I would prefer = NULL to be like the
ole' MS style or .clear()


But xyz.reset() is just the defaulted-argument special case of
xyz.reset(pointer-to-something). Either you give it a name that's
inappropriate for the more general case, or you have a proliferation of
member functions with overlapping effects.
to be like the STL folk.


who can't even decide whether a string has a .length() or a .size() ;-)
--
Richard Herring
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: sbobrows | last post by:
{Whilst I think much of this is OT for this newsgroup, I think the issue of understanding diagnostics just about gets under the door. -mod} Hi, I'm a C++ newbie trying to use the Boost regex...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
1
by: 张沈鹏 | last post by:
How to compile the HelloWorld of boost.asio? Maybe this is a stupid problem , but I really don't konw how to find the right way. My compile environment is WinXP, Msys , MinGw , G++ 3.4.2,...
11
by: Osiris | last post by:
I have these pieces of C-code (NOT C++ !!) I want to call from Python. I found Boost. I have MS Visual Studio 2005 with C++. is this the idea: I write the following C source file:...
1
by: Yahooooooooo | last post by:
Just practicing BOOST regular expressions....giving errors... -- wanted to replace SPACE with NULL. #include <iostream> #include <fstream> #include <sstream> #include <string> #include...
1
by: =?UTF-8?B?SmVucyBNw7xsbGVy?= | last post by:
(I also posted this to boost-user) The BGL implementation of breadth-first search uses a dedicated color map. I had the following idea: Some algorithms don't need to distinguish black/gray,...
1
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
2
by: Man4ish | last post by:
I have created Graph object without vertex and edge property.It is working fine. #include <boost/config.hpp> #include <iostream> #include <vector> #include <string> #include...
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.