Say I want do something for integral types but not chars and I have
is_integral<T>::type and is_char<T>::type
Is it possible to write this:
integral_constant<bool,is::integral<T>::value && !is_char<T>::value>
more readable
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Those kinds of metacomputations were done even before C++11 and Boost.MPL is the heavy artillery of TMP in C++03. With it, your requirements can be expressed like so:
(Where
and_andnot_are from theboost::mplnamespaces.)Notice the limited verbosity because you don’t have to put with the
::valueboilerplate. Similarly,resultis lazy, where you can force computing the result with eitherresult::value(which would betrueorfalse) orresult::type(which would be, well, a type — the documentation has all the details). Boost.MPL makes it easy not to do that though, so for instancenot_<result>is enough to invert the logic, even thoughnot_<result::type>would also work. A good thing considering that an additoinaltypenameis needed ifresultis dependent.I do consider Boost.MPL of enormous help in C++03 partly because it emulates variadic templates. For instance,
and_is not restricted to two arguments. I’ve shied away from it in C++11 however because in most situations where I used it I now use pack expansion. Things likeand_are still useful though because it is not possible to expand a pack in arbitrary expressions, e.g. in one that involves the&&logical operator. This can only be done with a logical metafunction:I consider this article a good read that gives good hints to reduce the verbosity of metacomputations. It focuses on SFINAE as used for generic programming but it is applicable to TMP as well (and SFINAE can be used in TMP as well). The final example is
which could look like this in C++03, using facilities similar to those provided in Boost:
A naive C++11 version arguably looks the worst:
I’ve seen some that advocate moving away from the style of Boost.MPL, which favours types and type-‘returning’ metafunctions towards using values and
constexprfunctions. This could look like:You’d still need a
constexprfunction to compute e.g. the logical disjunction ifT...is a pack though:EnableIf<any(is_foo<T>()...)>.