Template Metafunction If

Conditional metafunction returning the first typename if true, the second if false. A second conditional (if_c) metafunction returns the first constant if true, the second if false;

 namespace framework {
 namespace meta
 {
    template <bool C, typename T0, typename T1>  struct if_;
    template <typename T0, typename T1> struct if_<true, T0, T1> { typedef T0 value; };
    template <typename T0, typename T1> struct if_<false, T0, T1> { typedef T1 value; };

template <bool C, typename T, typename T C0, typename T C1> struct if_c; template <typename T, typename T C0, typename T C1> struct if_c<true, T, C0, C1> { static T value; }; template <typename T, typename T C0, typename T C1> struct if_c<false, T, C0, C1> { static T value; }; template <typename T, typename T C0, typename T C1> T if_c<true, T, C0, C1>::value = C0; template <typename T, typename T C0, typename T C1> T if_c<false, T, C0, C1>::value = C1;

} // namespace meta } // namespace framework

For an example, see TemplateMetafunctionHasFunction.


TemplateMetaprogrammingTechniques TemplateMetafunctionIdentity TemplateMetafunctionHasFunction CategoryCppTemplates


EditText of this page (last edited June 9, 2005) or FindPage with title or text search