
On Wed, Apr 2, 2008 at 7:18 PM, Balasubramanyam, Shivakumar
So example is,
typedef unsigned long hexType; hexType value; lexical_cast<HexType>("0xAABBCCDD");
How do we extend the boost::lexical_cast to support casting user defined types?
HexType isn't a UDT. If you make it a proper class/struct, then you can overload operator>> and lexical_cast will use that. Add in an implicit conversion to long and the syntax you used above might even work directly. Something like this: struct HexType { unsigned long x; operator unsigned long() { return x; } } istream &operator>>(istream &source, HexType const &h) { h.x = read_with_base_prefix<unsigned long>(source); return source; } And then your code will (probably) work typedef unsigned long hexType; hexType value; value = lexical_cast<HexType>("0xAABBCCDD"); HTH, ~ Scott ( P.S. but if you do it for real, use names that aren't different by case, maybe avoid the implicit conversion, etc )