c++ : meaning of keyword "typename" in this function -
this question has answer here:
in this answer this question author posted code:
template <typename... ts> typename std::tuple_element<0, std::tuple<ts...> >::type // or decltype(auto) callfunction(ts&&... ts) { using type = typename std::tuple_element<0, std::tuple<ts...> >::type; auto = multicache.find(typeid(type)); assert(it != multicache.end()); auto&& fn = boost::any_cast<const std::function<type(ts...)>&>(it->second); return fn(std::forward<ts>(ts)...); }
the meaning of typename std::tuple_element<0, std::tuple<ts...> >::type
returned type same of first element of first element in ts...
, right?
no, typename
keyword, practical purposes, means same thing class
.
the difference semantic. in context of template function or class, typename
can pod
, rather formally declared class. literally means "any type, pod or class", loosely speaking.
that's keyword typename
means in generally. in case, has specific purpose:
typename std::tuple_element<0, std::tuple<ts...> >::type
this tells compiler expect "type" in std::tuple_element<0, std::tuple<ts...> >
going class
(or typedef
), opposed class member.
when have looks like:
classname::identifier
this can refer either type or class member:
class x { public: typedef int y; int z; };
here, x::y
refers type, , x::z
refers class member. when comes parsing templates, c++ compiler assume default "a::b" going refer class member, unless stick typename
in front of it, in case it'll parsed type.
Comments
Post a Comment