|
Boost Users : |
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2008-04-02 20:32:55
On Wed, Apr 2, 2008 at 7:18 PM, Balasubramanyam, Shivakumar
<sbalasub_at_[hidden]> wrote:
>
> 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 )
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net