#include template struct Flags { typedef E EnumType; typedef S StorageType; StorageType value; struct Bitset { StorageType value; Bitset() { value = 0; } Bitset(EnumType T) { value = 1 << (int)T; } }; Flags() : value(0) { } Flags(Bitset A) : value(A.value) { } }; template bool operator&(Flags flags, typename Flags::Bitset bits) { return (flags.value & bits.value) != 0; } template bool operator==(Flags flags, typename Flags::Bitset bits) { return flags.value == bits.value; } template typename Flags::Bitset operator|(typename Flags::Bitset B, E A) { B.value |= 1 << A; return B; } template typename Flags::Bitset operator|(E A, E B) { return typename Flags::Bitset(A) | B; } enum Enum { One, Two, Three, Four, Five }; void main() { Flags flags(Two | Three); assert(flags & Three); assert(flags & Two); assert(flags & (One | Two)); assert(flags & (One | Three)); assert(flags & (One | Two | Three)); assert(!(flags & One)); assert(!(flags == Two | One)); assert(flags == Two | Three); flags = One | Five; assert(flags & One); assert(flags & Five); assert(!(flags & (Two | Three | Four))); }