Boost logo

Boost :

Subject: Re: [boost] Extrinsec conversions
From: Vicente Botet Escriba (vicente.botet_at_[hidden])
Date: 2009-04-01 08:41:10


Vladimir Batov wrote:
>
>> Do you think that it could be an interest in extending the C++0X
>> standard to take care of these extrinsically defined conversions
>> between unrelated types?
>
> To begin with I personally do not believe there is any capacity or time to
> look at extending C++0x. Already quite a few things are being re-scheduled
> for a later date.
>
> Second, I feel that in general terms conversions between "unrelated" types
> can only be done on individual basis and to be addressed by making those
> types "related", i.e. via Foo::Foo(Other const&) constructor or operator
> Other() converter.
>
> Another approach to generalizing conversion is only IMHO possible by
> defining/relying on a certain quality that the respective classes possess.
> That is, lexical_cast requires op>>() and op<<() for a class to become a
> member of the "club". Therefore, I do not feel that generalizing of such
> approach on the Standard level is possible.
>
>> Have a sens to have extrinsec conversions which implicitly convert?
>> Should this convert_to<T> operator be considered as a friend class of T?
>
> I am confused. I'd think that conversions that you call "extrinsec" are
> orthogonal to the participating classes and, therefore, always explicit,
> i.e.
>
> int i = str;
>
> will not compile when
>
> int i = lexical_cast<int>(str);
>
> will. And I would not expect such conversion facility (like convert_to or
> lexical_cast) to be friends to anyone as it seems intrusive.
>
> Just my 2c.
>
>

Hi,

thanks for your comments. You are right, there is no time for C++0x, may be
C++1y!

My proposal is exactly that make conversion on individual basis but sharing
the same syntax, i.e. when two types are unrelated we can always define a
specific function like

DestType toDestType(OrigType& orig);

What I purpose is to use always the convert_to function specialized with the
specific types. So the user needs to do

template <>
DestType convert_to<DestType,OrigType>(OrigType& orig);

Once we have this, generics can use the convert_to any convertible types.

template <typename T>
void f() {
  // ...
  OrigType v;
  T t = convert_to<T>(v);
  // ...
}

This will compile as far as either there is a constructor T(const
OrigType&), or a conversion operator OrigType::operator T(), or a
specialization as the above.

My concern relate to the C++ language was, why not share the current
converion syntax and let replace

  T t = convert_to<T>(v);

by

  T t = T(v);

In addition if the user declares the specific conversion implicit

template <>
implicit DestType convert_to<DestType,OrigType>(OrigType& orig);

this would mean that we can do just

  T t = v;

Hoping the purpose is more clear,
Vicente

-- 
View this message in context: http://www.nabble.com/Extrinsec-conversions-%28Was--safebool--Can-we-generalize-it-and-put-it-intoutilities-%29-tp22820099p22825567.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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