c++ - Style of programming templates -
if having option, 1 choose?
template<class checkeverynode<true>> struct x; or template<bool checkeverynode> struct x; that isn't obvious designers perspective, on 1 side have readability in actual code:
//taking first approach //somewhere in code x<checkeverynode<false>> x; on second side there more type , prefer:
//taking second approach //somewhere in code x<false> x;//but here don't see false means. so, looking forward opinions/suggestions
often dabbling in metaprogramming, can recommend "verbose" approach, don't quite first approach propose.
ideally, when using policies, don't pass flag, pass policy object, allow user customize @ will, rather relying on own predefined values.
for example:
struct nodecheckertag {}; struct checkeverynode { typedef nodecheckertag policytag; void check(list const& list); }; struct checkfirstnode { typedef nodecheckertag policytag; void check(list const& list); }; template <typename rand> struct checksomenodes { typedef nodecheckertag policytag; checksomenodes(rand rand): _rand(rand) {} void check(list const& list); rand _rand; }; your class should allow user choose policy elect:
template <typename nodechecker> class x: nodechecker // allow stateful implementation let ebo kick in { }; the policytag presence of several policies:
template <typename nodechecker, typename nodeallocator, typename nodenotifier> class x; you should provide sensible defaults, there case of i want customize last!, switching variadic template, can that:
template <typename tag, typename default, typename... policies> struct policyselector { typedef /**/ type; }; template <typename... policies> class x: policies... { typedef typename policyselector<nodecheckertag, checknonode, policies...>::type nodecheckerpolicy; typedef typename policyselector<nodeallocatortag, stdallocator, policies...>::type nodeallocatorpolicy; ... }; note inheriting policies, selection might unnecessary if care invoking functions. it's necessary if need inner typedefs hidden in policies should explicitly typedef'd in derived class (x here).
Comments
Post a Comment