Boost logo

Boost :

Subject: Re: [boost] [conversion] Motivation for two NEW generic conver_to and assign_to functions
From: Jeffrey Hellrung (jhellrung_at_[hidden])
Date: 2009-10-24 15:53:14


vicente.botet wrote:
>> Two comments (for now):
>>
>> 1) It's not uncommon that conversion to a "To" type from a "From" type
>> needs more information than just an instance of a From. One example
>> would be when dynamic memory allocation is necessary to construct the To
>> type, e.g., when converting from a boost::array to a std::vector.
>
> How do you know the To to which do you want to convert, if not given explictly?

It is given explicitly.

>> In my
>> application, I have to "upgrade" (i.e., convert) extended-but-fixed
>> precision floats (which are statically allocated) to arbitrary precision
>> floats (which are dynamically allocated), and the allocator_type of the
>> arbitrary precision float shouldn't generally be default-constructed.
>> I've worked around this by requiring my generic convert function to take
>> a ConvertPolicy parameter (defaulted to some empty struct
>> null_convert_policy_t), whose interpretation is up to the particular
>> overload chosen for the conversion. I wouldn't mind seeing a better
>> system in place. Have you considered this situation?
>
>
> I'm interested on seen how you have solved the problem you found. and please could you post your example and show why it can not be specialized with the proposed convert_to or assign_to interface? Non conforming use case will improve the final library.

I use overloads of the following signature:

template< class From, class To, class ConvertPolicy >
To convert(const From& from, type_tag<To>, const ConvertPolicy&
convert_policy)

struct type_tag is just an empty struct used to avoid having to provided
the "To" template parameter explicitly. I thought maybe a function
signature which didn't require explicit template parameters to call
might be more ADL-friendly, but admittedly I have nothing to back this
up with...

For (one of) my specific use cases, I need to convert among user-defined
numeric types, some of which are statically allocated and some of which
are dynamically allocated. Or even a conversion from double to some
dynamically-allocated numeric type might be desired. Either way,
converting to a dynamically-allocated numeric type (think an
arbitrary-precision rational) needs an allocator to construct it, so how
would you convert a double to an arbitrary_precision_rational< T,
allocator_type > without additionally providing an instance of
allocator_type?

To get around this issue, I ultimately pass an instance of the
allocator_type as the ConvertPolicy parameter, and it is interpreted
appropriately by the specific overload that implements the conversion
from double to arbitrary_prec...< T, allocator_type >.

Of course, this begs the question: How does the code that calls
"convert" know to pass allocator_type as the ConvertPolicy? It
generally doesn't, of course, so it also needs to be provided a
ConvertPolicy parameter by *its* client. At some point up the call
hierarchy (even if this has to be at main scope), it is known what
family of types one needs to convert among, hence the appropriate
ConvertPolicy would be known and initiated then.

Is that an adequate explanation?

>> 2) Assuming I understand the Conversion library correctly, what are the
>> advantages to using both overloading via ADL and function template
>> specialization as opposed to just the former? I'm not familiar of any
>> other free functions used in boost (or anywhere else, for that matter)
>> that rely on (perhaps I should say "allow"?) function template
>> specialization, so I'm wondering what the motivation and pros/cons are
>> (cons other than compiler support). I would guess that function
>> template specializations must reside in the same namespace as the
>> primary template...?
>
> The single think I can answer is that the same question is already on the documentation, unfortuantley with no answer.
> The answer I can give you now is that I don't master ADL to the point of using it to imlement what I have in mind. I'm unable to saying you if this is possible or not. The question of using partial specializations of template functions is for the moment secondary, if there is a better way to achieve the same goal. My main concern is if we need the generic functions convert_to and assign_to. If this can be implemented in a better way using only ADL, we could do this way. Of course I'm intersted in knowing how this can be implemented using only ADL, even if this is evident for most of you.
>
>> This is definitely a problem that needs to be solved (correctly) in a
>> generic fashion, so I'll try to follow your progress.
>
> I'm open to any sugestion. If we can implement it using only ADL, I will do it once I'll know how.

You might want to read Herb Sutter's GotW articles on ADL, linked from
Wikipedia:

http://en.wikipedia.org/wiki/Argument_dependent_name_lookup

> BTW, can we add functions on the 'std' namespace?

I've got the feeling that this is generally frowned upon, but not sure... :/

> Thanks for your comments and questions,
> Vicente

No problem.

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk