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

  • SEARCH
  • Home
  • 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 987109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:25:39+00:00 2026-05-16T05:25:39+00:00

In the description of some std template function I saw something like: if the

  • 0

In the description of some std template function I saw something like:

if the template parameter is of integral type, the behavior is such and such.
otherwise, it is such and such.

How can I do a similar test? Perhaps dynamic_cast?

Since the function I write is for my personal use I can rely on myself to supply only correct parameters, but why miss a chance to learn something? 🙂

  • 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-05-16T05:25:40+00:00Added an answer on May 16, 2026 at 5:25 am

    In addition to the other answers, it should be noted that the test can be used at runtime but also at compile-time to select the correct implementation depending on wether the type is integral or not:

    Runtime version:

    // Include either <boost/type_traits/is_integral.hpp> (if using Boost) 
    // or <type_traits> (if using c++1x)
    // In the following, is_integral shoudl be prefixed by either boost:: or std::
    
    template <typename T>
    void algorithm(const T & t)
    {
        // some code
    
        if (is_integral<T>::value)
        {
            // operations to perform if T is an integral type
        }
        else
        {
            // operations to perform if T is not an integral type
        }
    
        // some other code
    }
    

    However, this solution can be improved when the implementation of the algorithm greatly depends on the test. In this case, we would have the test at the top of the function, then a big then block and a big else block. A common approach in this case is to overload the function and make the compiler select the correct implementation using SFINAE. An easy way to do this is to use boost::enable_if:

    #include <boost/utility/enable_if.hpp>
    #include <boost/type_traits/is_integral.hpp>
    
    template <typename T>
    typename boost::enable_if<boost::is_integral<T> >::type
    algorithm(const T & t)
    {
        // implementation for integral types
    }
    
    template <typename T>
    typename boost::disable_if<boost::is_integral<T> >::type
    algorithm(const T & t)
    {
        // implementation for non integral types
    }
    

    When invoking the algorithm function, the compiler will “select” the correct implementation depending on wether the template parameter is integral or not.

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

Sidebar

Related Questions

I have a text like: Title A some description on a few lines, there
There is some type Record : type Day = Integer type Description = String
How can i easily parse a document which has this structure description some line
So I have such function: boost::shared_ptr<my_class> get_class_by_name(std::string name) { typedef std::map<boost::shared_ptr<my_class>, my_description> map_t; BOOST_FOREACH(map_t::value_type
Let's say I have a template like this <script id=rowTemplate type=text/x-jquery-tmpl> <tr> <a href=${
Given a JSON string like this: {Locations: {list: [ {description: some description, name: the
According to MSDN BOL (Books Online) description on SOME | ANY (Transact-SQL) , SOME
How to find below comment block(s) with javascript/jquery regex: /* some description here */
I having two arrays say, $array1 = array(code => E0089, desc => some description);
FYI, There is some overlap in the initial description of this question with a

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.