c++ - Template function return type deduction in C++03 -
i implement following in c++03:
template <typename t1, typename t2> t1 convert(bool condition, t2& value) { return condition ? value : conversionfunction(value); }
except call convert
without having explicitly specify t1
. how do that?
perhaps use hack. hack defer conversion until use return value. return value helper object allows converted else.
template <typename t> struct converthack { t &value_; const bool cond_; converthack (bool cond, t &value) : value_(value), cond_(cond) {} template <typename u> operator u () const { return cond_ ? value_ : conversionfunction(value_); } }; template <typename t> converthack<t> convert(bool condition, t& value) { return converthack<t>(condition, value); }
Comments
Post a Comment