
On 05/03/2011 11:41 AM, Jeff Flinn wrote:
I personally don't have those reservations and the applicability of Make. Even more concise with perhaps less preconceptions might be:
as<int>(s, 123);
Just a data point: I've been using 'obj.as<U>' as my own convention for a few years now and I like it. It was a little easier on a "lenient" compiler where I started using it: struct mytype { // Unimplemented general template. template <class U> U as() const { BOOST_MPL_ASSERT_MSG(false, NO_CONVERSION_TO, (U)); } // specializations define the types we support "conversions" to: template <> std::string as() const {return this->to_string_imp(); } template <> std::wstring as() const {return this->to_wstring_imp();} ...possibly more... ... }; template <class T> T f() { mytype m(...); return m.as<T>(); } Just tried it again the other day with a class template on a more conforming compiler (which required member specializations at namespace scope). It was a lot more overhead code to make it work. If this library could help me define explicit conversions "as" member templates with a sane amount of boilerplate again, I'd be a fan. - Marsh