0

A particular template pattern I recently encountered uses boolean type check to determine the specialization to invoke. The pattern looks quite familiar to Policy based design except that the Policy parameter are controlled by the data type rather than making it configurable.

template<
    typename Ty, 
    bool as_type = is_associative<Ty>::value,
    bool it_type = is_iterable<Ty>::value,
    bool pr_type = is_pair<Ty>::value
>
struct brackets
{
    // Any other case is an Error
};
template<typename Ty>
struct brackets<
    Ty,
    true,  //Associative
    true,  //Iterable
    false  //But not a Pair
>   {
    static const char open = '{', close = '}';
};
template<typename Ty>
struct brackets<
    Ty, 
    false, //Not Associative
    true,  //But Iterable
    false  //But not a Pair
>   {
    static const char open = '[', close = ']';
};
template<typename Ty>
struct brackets<
    Ty,
    false, //Neither Associative
    false, //Nor Iterable
    false  //Nor a Pair
>   {
    static const char open = '<', close = '>';
};
template<typename Ty>
struct brackets<
    Ty,
    false, //Neither Associative (actually don't care)
    false, //Nor Iterable
    true   //But a Pair
>
{
    static const char open = '(',  close = ')';
};

Can the community help me to identify the pattern?

4
  • Why do you care if it has a name? Aug 16, 2014 at 13:53
  • Why do you care? Don't obsess over design patterns. Maybe this isn't 100% conformant to some strict list of design patterns that some hotshot wrote in a book. So what? Does it work? Is it clear what it's doing? Is it easy to maintain? Yes? Then move on. Aug 16, 2014 at 13:57
  • 1
    @LightnessRacesinOrbit: The reason I do care is because with a name in place, its easier to search, communicate, document and improve (if required).
    – Abhijit
    Aug 16, 2014 at 17:43
  • @Abhijit: Easier to pigeonhole and focus on marketing buzzwords over actual code quality, you mean Aug 17, 2014 at 22:02

1 Answer 1

0

I would say this is most closely related to the type traits technique identified by Alexandrescu in his Modern C++ design.

This class makes compile-time decisions based on the type parameter, customizing the code based on properties of Ty.

2
  • I am not sure if I can agree with you on this, but thanks any way for your answer.
    – Abhijit
    Aug 16, 2014 at 17:45
  • What you describe is a fundamental fact of templates. I don't think you can call it a "pattern"... Aug 17, 2014 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.