c++ - Usage of typename in typedef versus inheritance -
just wondering c++ typename, used tell parser symbols types. why must use after typedef , not inheritance?
example: imagine have this
struct c { /* class */ }; template<class t> struct s { typedef c type; // s<t>::type type }; what's bothering me this:
template<class t> struct typedef { typedef typename s<t>::type mytype; // needs typename }; template<class t> struct inheritance : s<t>::type // doesn't need typename { }; in both cases parser should expect type, parse s<t>::type one. why inheritance, , not typedefs? pattern seems same me:
typedef $type$ $new_symbol$; class $new_symbol$ : $type$ { $definition$ }; or there usage of typedef i'm not aware of, make ambiguous?
ps: i'm pretty sure has been asked, can't find (there's lot of noise related typename keyword). question syntax, not whether it's better use inheritance or typedefs. appologize in advance if there's duplicate.
the reason typedef syntax more variable inheritance syntax. normally write typedef first, , type name second. order unimportant. wit, following valid typedef:
int typedef integer_type; now consider happens if use dependent name:
s<t>::type typedef integer_type; without doing non-trivial lookahead, parser cannot know s<t>::type refers type name here (because hasn’t yet seen typedef), disambiguation rules infers value. consistency in grammar, there no special case prefixed typedef (which, right, is unambiguous). there special case, there isn’t.
Comments
Post a Comment