Click here to Skip to main content
15,886,362 members
Articles / Mobile Apps
Article

boolean_cast

Rate me:
Please Sign up or sign in to vote.
4.39/5 (15 votes)
4 Dec 20052 min read 47K   214   11   6
Performs conversions between values of type bool, BOOL and VARIANT_BOOL.

Motivation

We can't ignore compiler warnings, but they are sometimes annoying:

void motivation(HWND hWnd)
{
  bool b = ::IsWindow(hWnd); // warning C4800
}

The above code produces the warning:

warning C4800: 'BOOL' : forcing value to bool 'true' 
                       or 'false' (performance warning)

static_cast makes the same warning as the one given above. Then you write:

void wrong(HWND hWnd)
{
  bool b = (::IsWindow(hWnd) == TRUE);
}

The behavior is undefined, because the Win32 document says:

If the window handle identifies an existing window, 
                             the return value is nonzero.
If the window handle does not identify an existing window, 
                             the return value is zero.

You may think that BOOL is defined as TRUE or FALSE, but it seems wrong. Then you write:

void right(HWND hWnd)
{
  bool b = (::IsWindow(hWnd) != 0);
}

Your job is completed (you can write FALSE as 0). Anyway, you must be thinking it is still annoying. So that is where boolean_cast comes in.

Requirements

boolean_cast

boolean_cast that is defined in the namespace poost safely converts values between bool, BOOL and VARIANT_BOOL types:

void good(HWND hWnd)
{
  bool b = boolean_cast<bool>(::IsWindow(hWnd));
}

VARIANT_TRUE is defined as ((short)-1), in fact, the implicit cast is dangerous:

void good_save()
{
  VARIANT_BOOL vb = TRUE;
  ATLASSERT( vb != VARIANT_TRUE ); // wow!

  vb = boolean_cast<VARIANT_BOOL>(TRUE);
  ATLASSERT( vb == VARIANT_TRUE );
}

boolean_cast increases the safety and readability. But well, why should you write target types? The compilers must know the types.

auto_boolean

auto_boolean uses the type of the assigned object as the target type:

void best(HWND hWnd)
{
  bool b = auto_boolean(::IsWindow(hWnd));
}

That's what you want. No warning, no annoying. Note that auto_boolean doesn't work under broken compilers like VC6 or eVC4.

How it works

The implementation of boolean_cast is as follows:

template< class TargetT, class SourceT >
TargetT boolean_cast(SourceT b)
{
  typedef boolean_converter<TargetT, SourceT> converter_t;
  return converter_t::convert(b);
}

boolean_cast delegates its task to boolean_converter class template. The real task is performed by the template. Let's look at the implementation:

// primary template
template< class TargetT, class SourceT >
struct boolean_converter;

// full specialization
template< >
struct boolean_converter<bool, BOOL>
{
  static bool convert(BOOL b)
  {
    return b ? true : false;
  }
}

The task is sent to "full class template specialization" depending on two types, TargetT and SourceT.

Next, let's look into auto_boolean. It is somewhat technical:

template< class SourceT >
struct auto_boolean_type
{
  explicit auto_boolean_type(SourceT b) : m_b(b)
  { }

  // conversion operator
  template< class TargetT >
  operator TargetT() const
  { return boolean_cast<TargetT>(m_b); }

private:
  SourceT m_b;
};

template< class SourceT >
auto_boolean_type<SourceT> auto_boolean(SourceT b)
{
  return auto_boolean_type<SourceT>(b);
}

It is interesting that auto_boolean returns not a type but auto_boolean_type. The compilers must convert auto_boolean_type to the target type. They find and call the "conversion operator" and at last boolean_cast is automatically invoked instead of you invoking it.

Points of interest

reinterpret_cast and static_cast get a lot of use in most programs. It can be time consuming to determine what each cast is doing after you haven't looked at your code for several months, and a domain-specific cast such as boolean_cast can help you reduce this time.

Tested under

References

History

  • 5th December, 2005: Initial version uploaded.
  • 6th December, 2005: Document updated.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Japan Japan
I am worried about my poor English...

Comments and Discussions

 
QuestionImprovement Pin
TheToeJoe18-Nov-11 9:07
TheToeJoe18-Nov-11 9:07 
GeneralHeavy way! Pin
ZhaoRuFei5-May-09 23:16
ZhaoRuFei5-May-09 23:16 
GeneralRe: Heavy way! [modified] Pin
frugalmail23-Oct-09 13:17
frugalmail23-Oct-09 13:17 
GeneralSmall improvements. Pin
David O'Neil4-Dec-05 16:56
professionalDavid O'Neil4-Dec-05 16:56 
I have gotten the "forcing value to bool 'true' or 'false'" warning at level 3. Of course, you would also get them at level 4.

I would rephrase your 'Points of interest', if I were you. Maybe something like:

"reinterpret_cast and static_cast get a lot of use in most programs. It can be time consuming to determine what each cast is doing after you haven't looked at your code in several months, and a domain-specific cast such as boolean_cast can help reduce this time."

(Technically, using static_cast and reinterpret_cast is NOT the same as using C-style casts. You say it is the same, and this decreases your authority to those that know better.)

You might also want to add a short discussion about how boolean_cast works. It doesn't have to be much - just enough detail to make the reader go "Oh! I can grasp how it works now."

Happy coding!
David

-- modified at 22:56 Sunday 4th December, 2005

ps - redo your intro. It doesn't lead the reader into the subject - it just drops him/her in the middle of it, no explanation. "I was coding the other day, and I got the "forcing value to bool 'true' or 'false'" warning from VC7.1, and I thought "This is a stupid warning..." (Or however you wish to do it.)

Please don't take these suggestions in a negative way. I know how much effort it takes to write an article, and you have posted a method that is useful, and my hat is off to you for writing this. Your English is better than my Japanese Smile | :) If you want more feedback, just post a thread here, and maybe the other members can give some advice.


-- modified at 1:20 Thursday 22nd December, 2005

Much better. Smile | :)
GeneralRe: Small improvements. Pin
mb2sync5-Dec-05 5:14
mb2sync5-Dec-05 5:14 
GeneralRe: Small improvements. Pin
David O'Neil5-Dec-05 5:26
professionalDavid O'Neil5-Dec-05 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.