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 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 would like to create a file whose descriptor would have some customizable behavior.
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}
I've read the description of functionoids here . They look like a poor-man's version
Is there a description of the various errors that NSXMLParser can return? I'm aware
C++0x thread library or Boost.thread define non-member variadic template function that lock all lock
For whatever reason, std::cout does not display anything with my application. The description of
I've got some WSDL description of a protocol which I need to implement (->
Based on the following table Table_A ID Rev Description ----------------------------------- 1 1 Some text.
I want to create some description of menu bars (items and connected with them
Description | A Java program to read a text file and print each of

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.