Don’t use CArchive with native C++ type bool

I recently ran into an issue where I was trying to serialize a bool variable from a CArchive object.

Archiving it out, with this code, works fine:

//CArchive ar;
bool bFill;
ar << bFill;

But this code:

ar >> bFill

has problems. It compiles fine under Visual Studio 2003, but errors out under Visual C++ 6 with this error:

C2679: binary ‘>>’
: no operator defined which takes a right-hand operand of type ‘bool’
(or there is no acceptable conversion)

Very weird. There is some explanation here.

My resolution? Use BOOL instead. Sure, it’s actually an int and takes 4 bytes, not 1, but that’s a not a big deal in this case. And it’s more clear than using BYTE.

2 thoughts on “Don’t use CArchive with native C++ type bool

  1. Michael

    Try ading this somewhere in your headers:

    #if _MFC_VER != 0x700

    inline CArchive& AFXAPI operator >> (CArchive& Archive, bool &Value)
    Archive.Read(&Value, sizeof(bool));
    return Archive;

    inline CArchive& AFXAPI operator

  2. pepethecow Post author

    Unfortunately, the project did have to compile under VC6, and I wanted to keep the code as consistent as possible. Thankfully, we recently removed support for the older compiler so I don’t have to worry about this anymore.

Comments are closed.