c++11 - Is there a way to cast Template's Type to shared_ptr<T>? -
recently, working school's assignment construct simple polynomial expression using class , object. don't have construct parse function , construct normal expression there's lot codes needed write , hard discern when there's many codes. thought maybe context try c++ template ( i'm fresh man c++, not experienced template , not sure if in condition use it.) example, need implement operatorplus declaration correspondant isstd::shared<expression> operateurplus(std::shared<expression>, std::shared<expression>)
. want create template<typename t, typename m> std::shared<expression> plus(t lhs, m rhs)
wrapper response different passed-in parameters. , follow clause in language add type limitation utilizing enable_if
. code this:
template<typename t, typename m, typename = std::enable_if<( std::is_same<unsigned int, t>::value || std::is_same<char, t>::value || std::is_same<std::shared_ptr<expression>, t>::value) && ( std::is_same<unsigned int, m>::value || std::is_same<std::shared_ptr<expression>, m>::value || std::is_same<char, m>::value)> > std::shared_ptr<expression> plus(t lhs, m rhs){ std::shared_ptr<expression> t_lhs, t_rhs; if (std::is_same<t, uint>::value) t_lhs = nombre::create(uint(lhs)); if (std::is_same<t, char>::value) t_lhs = std::shared_ptr<expression>(new variable(char(lhs))); if (std::is_same<t, std::shared_ptr<expression>>::value) t_lhs = (std::shared_ptr<expression>)(lhs); if (std::is_same<m, uint>::value) t_rhs = nombre::create(uint(rhs)); if (std::is_same<m, char>::value) t_rhs = std::shared_ptr<expression>(new variable(char(rhs))); if (std::is_same<m, std::shared_ptr<expression>>::value) t_rhs = (std::shared_ptr<expression>)(rhs); return std::shared_ptr<expression>(new operateurplus(t_lhs, t_rhs)); }
and question (std::shared_ptr<expression>)(lhs)
part. used c-style casting because don't how realize casting operation. ide told std::shared_ptr not pointer or reference if tried static_cast> , think lhs unsigned int type. if follow compile's hints, question is
how cast template's type std::shared_ptr? or
if possible pass std::shared_ptr template parameter?
the body of template function must entirely compilable given template instantiation. if 1 of if
statements never visited, condition still needs syntactically valid appropriate types.
having single function wrong approach. 1 possible solution create overloaded function std::shared_ptr
of required sources, use function achieve desired genericicity.
using expptr = std::shared_ptr<expression>; //for brevity expptr converttoexp (expptr e) { return e; } expptr converttoexp (unsigned int i) { return nombre::create(i); } expptr converttoexp (char c) { return std::make_shared<variable>(c); } template <typename t, typename u> expptr plus (t lhs, u rhs) { auto lhsexp = converttoexp(lhs); auto rhsexp = converttoexp(rhs); return std::make_shared<operateurplus>(lhsexp, rhsexp); }
i don't think sfinae necessary. hard-fail if there isn't valid call converttoexp
t
or u
.
i didn't try compile didn't supply mvce, there might mistakes.
if want sfinae prevent implicit conversions in converttoexp
calls, in cleaner way this:
template<typename... conds> struct or_ : std::false_type {}; template<typename cond, typename... conds> struct or_<cond, conds...> : std::conditional_t<cond::value, std::true_type, or_<conds...>> {}; template <typename t, typename... ts> using is_one_of = or_<std::is_same<t,ts>...>; template <typename t> using is_valid_exp_source = is_one_of<t, char, unsigned int, std::shared_ptr<expression>>; template <typename t, typename u> std::enable_if_t<is_valid_exp_source<t>::value && is_valid_exp_source<u>::value, expptr> plus (t lhs, u rhs) { auto lhsexp = converttoexp(lhs); auto rhsexp = converttoexp(rhs); return std::make_shared<operateurplus>(lhsexp, rhsexp); }
Comments
Post a Comment