Boost logo

Boost :

Subject: Re: [boost] [conversion] Motivation for two NEW generic conver_to and assign_to functions
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-10-26 16:46:05

vicente.botet wrote:
> I have modified the convert_to implementation with your
> suggestions (see below) and now we can do:
> B b;
> A a1 (convert_to(b, type_tag<A>() ));
> The user can add overloading on the namespace of the class
> From, or specialize the boost::conversion::convert_to function.
> I have defined the tag with a defaulted parameter value
> type_tag<Target> tag=type_tag<Target>(), so we can also do
> A a1 (convert_to<A>(b));

This expansion of use cases will make the function more confusing and, I think, increase the opportunities to create ambiguities.

I fail to understand why

   convert_to(b, type_tag<A>())

is acceptable and


is not in any circumstance.

The former is more verbose and both require the template parameter A.

I understand that the former uses ADL to find the appropriate overload of a normal function, whereas the latter uses a function template specialization to define the function. I also understand that the former can appear in B's or A's namespace (or both!), whereas the latter must be in the boost::conversion namespace. What I don't understand is why the former is preferable to the latter, or why both are deemed necessary.

For the latter version, the author of A, aware of B, could provide convert_to<A,B>() and the author of B could provide a generic version that might work for A, thus creating an ambiguity. Likewise, for the former variant, an overload could be declared in both A's and B's namespace. Thus, both approaches can lead to ambiguities, making neither better than the other. Indeed, supporting both would likely lead to even more chances for ambiguity.

> I have added still an overloading to simplify some uses as
> a = convert_to(b, a);
> The Target parameter is not used other than to retrieve the type.
> template <typename Target, typename Source>
> Target convert_to(Source const& from, Target const&);

That form doesn't make sense to me. If you already have an object of type Target to pass as the second argument, why not just use the following signature?

   template <typename Target, typename Source>
   convert_to(Target & _dest, Source const & _from);

That keeps the Target and Source values in the same order as the other variations (Target is on the lhs of the value-returning variations) and avoids the duplication of return value and argument.

That function might be easier to use in some cases were it declared like this:

   template <typename Target, typename Source>
   Target &
   convert_to(Target & _dest, Source const & _from);

(It would simply return _dest after assigning the converted value to _dest.)

Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Boost list run by bdawes at, gregod at, cpdaniel at, john at