Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2008-04-03 13:24:42


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_at_[hidden]> 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_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Scott McMurray
> Sent: Wednesday, April 02, 2008 5:33 PM
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] [lexical_cast] for user type extensions.
>
> 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 mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



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