Extrinsec conversions (Was [safebool] Can we generalize it and put it intoutilities?)

----- Original Message ----- From: "Eugene Wee" <crystalrecursion@gmail.com> To: <boost@lists.boost.org> Sent: Wednesday, April 01, 2009 4:10 AM Subject: Re: [boost] [safebool] Can we generalize it and put it intoutilities?
Hi,
On Wed, Apr 1, 2009 at 6:20 AM, Vladimir Batov <vladimir.batov@wrsa.com.au>wrote:
I glanced over "explicit conversion operators" in C++0x. It did not struck me as relevant to safe_bool. Care to elaborate?
It is relevant since you can say, define an explicit conversion function to convert to bool, and yet avoid the very problems with say, also allowing implicit conversion to int, that the safe bool idiom is used to avoid. In other words, it is a language feature directly designed to replace the safe bool idiom, and more.
Hi, Thanks for the clarification. Even if the following is not related to the safe bool idiom it is related to conversions. The current standard allows to define intrinsec coversions, i.e. conversions that are defined either as a conversion operator on the from class or as a constructor on the to class. Vladimir StringConvert proposal seems to go towards a conversion from/to strings (If I've understood?). lexical_cast can be seen as an extrinsec converions of unrelated types via a common type stream type. I have proposed a generic convert_to template function that generalize extrinsicaly the explicit convertion of unrelated types. template <typename To, typename From> To convert_to(From&); One difference is that for C++0x explicit conversion you can write From from; To to=To(from); While for unrelated types you are forced to write From from; To to=convert_to<To>(from); The other difference is that constructors and conversion operators can be implicit, while the convert_to function can not. 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? 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 know that this is out of the scope of Boost, but I just wanted to know your opinion before posting to comp.lang.c++.moderated. Apologize if this has already been discused. Best, Vicente

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. V.

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-general... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (3)
-
Vicente Botet Escriba
-
vicente.botet
-
Vladimir Batov