Shiva,

As Scott mentioned you can oveload your shift operators for the HexType.
Overloading these operators in the namespace of HexType will cause ADL to choose them, but there is no guarantee that boost::lexical_cast will always use them. And if it changes, it might silently break your code.

About adding code to std or boost namespaces you might read this interesting discussion:
http://groups.google.ca/group/comp.lang.c++.moderated/browse_thread/thread/b396fedad7dcdc81


With Kind Regards,
Ovanes

On Thu, Apr 3, 2008 at 6:57 PM, Balasubramanyam, Shivakumar <sbalasub@qualcomm.com> wrote:
Hi scott,

Thanks for your input. I agree with your solution as well.

The other questions is the code location/namespace, this would work in 2
ways,

1. create the lexical_cast<HexType> in boost namespace. So the user is
transparent as to where it is getting from

2. we have a csw namespace in our project. We could create
csw::lexical_cast<HexType>

In the C++ conference, BJarne Stroutsup mentioned that it is bad idea to
put any user code in std or boost namespace. So that rules out number 1.

So I would like to know, how folks extend boost libraries for user
types. Please let me know if I am not clear.

My motivation is that, I would like folks to still refer to boost
website for api references, but I could add support for custom types


Thanks,
Shiva

-----Original Message-----
From: boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Scott McMurray
Sent: Wednesday, April 02, 2008 5:33 PM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] [lexical_cast] for user type extensions.

On Wed, Apr 2, 2008 at 7:18 PM, Balasubramanyam, Shivakumar
<sbalasub@qualcomm.com> 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 mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users