Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:48:04+00:00 2026-06-12T02:48:04+00:00

Say I want do something for integral types but not chars and I have

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:48:05+00:00Added an answer on June 12, 2026 at 2:48 am

    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:

    // in C++03:
    // typedef /* compute */ result;
    using result = and_<is_integral<T>, not_<is_char<T>>>;
    

    (Where and_ and not_ are from the boost::mpl namespaces.)

    Notice the limited verbosity because you don’t have to put with the ::value boilerplate. Similarly, result is lazy, where you can force computing the result with either result::value (which would be true or false) or result::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 instance not_<result> is enough to invert the logic, even though not_<result::type> would also work. A good thing considering that an additoinal typename is needed if result is 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 like and_ 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:

    // Enforce precondition: every type T must be integral
    static_assert( and_<std::is_integral<T>...>::value, "Violation" );
    

    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

    template <typename T,
              EnableIf<is_scalable<T>, is_something_else<T>>...>
    T twice(T t) { return 2*t; }
    

    which could look like this in C++03, using facilities similar to those provided in Boost:

    template<typename T>
    typename enable_if<
        and_<is_scalable<T>, is_something_else<T>>
        , T
    >::type twice(T t) { return 2*t; }
    

    A naive C++11 version arguably looks the worst:

    template<typename T>
    typename std::enable_if<
        is_scalable<T>::value && is_something_else<T>::value
        , T
    >::type twice(T t) { return 2*t; }
    

    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 constexpr functions. This could look like:

    // EnableIf alias now accepts non-type template parameters
    template<typename T
             , EnableIf<is_scalable<T>() && is_something_else<T>()>...>
    T twice(T t) { return 2*t; }
    

    You’d still need a constexpr function to compute e.g. the logical disjunction if T... is a pack though: EnableIf<any(is_foo<T>()...)>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have something like this: <p> <br><br> <img id='unique'> <br> </p> I want
Let's say I want to do something like this void my_printf(char *fmt,...) { char
lets say we have a link <a href=/something target=_new>blah</a> and now i want a
Say I want to write a function like this: int get_some_int(string index) { ...perform
This is not a major problem, just something I want to know whether or
Say I have: >which -a foo /bin/foo /usr/bin/foo I want something like: >foo Warning:
I want something similar to Mysql's sorting of data, but in jquery. I have
Say I have the classic 4-byte signed integer, and I want something like print
I want something similar to this post but with Visual Studio. This would be
Say I have the following html: <div id=myDiv title=My Div>This is me!</div> I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.