Boost logo

Boost :

From: Iain Denniston (iain.denniston_at_[hidden])
Date: 2007-08-14 05:42:36


[Uploaded to the vault (flags_v1.zip) – complete with docs and test file
(no jam file yet – sorry) - tested in vc8 and MinGW GCC 3.4.2.]

I’ve proposed this before and gotten a little interest – so I figured
I’d try again (particularly now that 1.34.1 is out) and see if I could
get some more interest! At its most simple, it’s a way to stop function
users providing difficult to detect erroneous data. The best way to work
out what this lib does is probably read the docs I uploaded, but for
here I’ll take one of the examples I use from the docs:

using boost::flags

enum foo
{
        foo_1 = 1<<0,
        foo_2 = 1<<1,
};

enum bar
{
        bar_1 = 1<<2,
        bar_2 = 1<<3,
};

void func1(unsigned int options)
{
        //do stuff
}

void func2(flags<foo> options)
{
        //do stuff
}

int main(int argc, char * argv[])
{
        func1(foo_1 | foo_2 | bar_1 | bar_2); //Compiles fine
        func2(flags<foo>(foo_1) | foo_2 | bar_1 | bar_2); //Compile time error
- cannot convert bar to foo
}

Additionally, sometimes it is useful to force the user to supply a flag
rather than allow them to supply none (i.e. '0') - the flags_no_default
provides this functionality.

The library is not very big and it’s not very clever :) but the hope is
to push errors back to compile time, at little or no runtime cost.

If people are interested or not either way it’s cool – but since it’s
now in the vault anyone can use it if they want.

Hope it all makes sense! Questions/comments/criticism gratefully received.

IAIN


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk