Boost logo

Boost :

Subject: Re: [boost] function convert() -- do you find it useful?
From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2014-06-29 15:25:25


2014-06-29 10:15 GMT+02:00 Vicente J. Botet Escriba <
vicente.botet_at_[hidden]>:

> Le 28/06/14 23:34, Andrey Semashev a écrit :
>
> On Sun, Jun 29, 2014 at 12:58 AM, Andrzej Krzemienski
>> <akrzemi1_at_[hidden]> wrote:
>>
>>> Hi,
>>> This is the second time that I have found the following function useful
>>> when playing with variants of Optional library:
>>>
>>> template <typename T, typename U>
>>> typename std::enable_if<std::is_convertible<U, T>::value, T>::type
>>> convert(U && u)
>>> { return std::forward<U>(u); }
>>>
>>> It performs an implicit conversion from U to T, but you can call it
>>> explicitly.
>>>
>> Isn't it what implicit_cast does? See boost/implicit_cast.hpp.
>>
>>
>> Andrey, is implicit_cast SFINAE friendly?

I haven't check it, but it looks like it is. The conversion is performed
outside the function so it should work with decltype(expression) trick. It
looks better to my implementation as it does not require enable_if or
forward. But it looks implicit_cast prevents copy elision (you cannot elide
from function parameter), so it incurs the cost of one move construction.

Regards,
&rzej


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