
On 11/04/12 19:57, Antony Polukhin wrote:
But that is not an optimal solution. Optimal solution would look like (not tested, required just to get the idea):
template<class Target, class Source>
Target lexical_cast_ext(Source&& s) {
Target t;
if (!::boost::try_lexical_cast(t, std::forward<Source>(s))) // No ADL BOOST_LCAST_THROW_BAD_CAST(Source, Target);
return t; // RVO must be applied by compiler
}
template<class Target, class Source>
bool try_lexical_cast_ext(Target& t, Source&& s) noexcept {
using namespace boost::detail; // For getting default `construct_lexical_cast_in_trait' and `construct_lexical_cast_out_trait'
const auto& in = construct_lexical_cast_in_trait(std::forward<Source>(s)); // ADL
if (in.fail()) return false;
typedef boost::mpl::identity<Target> target_tag;
// Must have up to 4 construct_lexical_cast_out_trait // {with parameters const char*, const wchar_t*, const char16_t*, const char32_t*}
return construct_lexical_cast_out_trait(target_tag(), in.begin(), in.end()) // ADL
.assign_value(t); // returns true if conversion is OK
}
User will need to add one `construct_lexical_cast_in_trait' and up to four `construct_lexical_cast_out_trait' functions to the namespace of user-defined class.
This will allow us to :
* get correct function pointers via&lexical_cast<Source, Target>; (lexical_cast overloading solution breaks that)
* use stream operators<< and>> in default lexical_cast traits
* tune lexical_cast for fast conversions to string types(deduce_char_traits<> specializing solution does not solve that)
* tune lexical_cast for fast conversions from string types(both solutions do not solve that)
* convert from any type to any type through usage of any character type (both solutions do not solve that)
* relax all the type requirements (current implementation does not allow that)
* have a noexcept version (current implementation does not allow that)
Any objections? Did I miss something except conversion tags for specifying base?
Why is lexical_cast so complicated and full and quirks? All that stuff looks messy and over-engineered.