Sorry - the expression 'compile-time' below is (of course) a misnomer as the table is implemented as a static array declared inside a static function. What I meant to say is that you declare the contents of the table together with the table definition, and that its contents are then fixed at run-time.
// Johan "Johan Nilsson" <[EMAIL PROTECTED]> wrote in message asf9bt$7g6$[EMAIL PROTECTED]">news:asf9bt$7g6$[EMAIL PROTECTED]... > Hi, > > I've been implementing some kind of compile-time initialized lookup table, > but it suddenly struck me: are there already similar functionality available > in boost? See attached sample code below. > > // Johan > > ------------------- > #incude <cassert> > #include <stdexcept> > #include <string> > #include "StaticLookupTable.h" > > enum Colors > { > Red = 1, > Blue = 2 > }; > > struct ColorTable > : public StaticLookupTable<ColorTable, Colors, std::string> > { > LOOKUP_TABLE_BEGIN() > LOOKUP_ENTRY(Red, "Red") > LOOKUP_ENTRY(Blue, "Blue") > LOOKUP_TABLE_END() > }; > > int main() > { > assert("Red" == ColorTable::Lookup(Red)); > assert(Blue == ColorTable::ReverseLookup("Blue")); > > try > { > ColorTable::Lookup(static_cast<Colors>(7)); > assert(!"Lookup of invalid color succeeded"); > } > catch(std::range_error&) > { > } > > return 0; > } > > > > > > _______________________________________________ > Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost > _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost