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 3614212
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:13:13+00:00 2026-05-18T22:13:13+00:00

I was watching the latest C9 lecture and noticed something interesting.. In his introduction

  • 0

I was watching the latest C9 lecture and noticed something interesting..

In his introduction to type_traits, Stephan uses the following (as he says, contrived) example:

template <typename T> void foo(T t, true_type)
{
    std::cout << t << " is integral";
}
template <typename T> void foo(T t, false_type)
{
    std::cout << t << " is not integral";
}

template <typename T> void bar(T t) { foo(t, typename is_integral<T>::type()); }

This seems to be far more complicated than:

template <typename T> void foo(T t)
{
    if(std::is_integral<T>::value)
        std::cout << "integral";
    else
        std::cout << "not integral";
}

Is there something wrong with the latter way of doing it? Is his way better? Why?

Thanks.

  • 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-18T22:13:14+00:00Added an answer on May 18, 2026 at 10:13 pm

    The example below should illustrate the difference. Let’s add struct X:

    struct X
    {
      X(int)
      {
      }
    };
    

    and modify one foo as below:

    template <typename T> void foo(T t, true_type)
    {
        std::cout << t << " is integral";
        X x(t);
    }
    template <typename T> void foo(T t, false_type)
    {
        std::cout << t << " is not integral";
    }
    

    Then:

    template <typename T> void bar(T t)
    {
        foo(t, typename is_integral<T>::type());
    }
    

    Will still compile for all T types (including integer types; it may cause warning but will compile).

    The other equivalent code:

    template <typename T> void foo(T t)
    {
        if(std::is_integral<T>::value)
        {
            std::cout << "integral";
            X x(t);
        }
        else
            std::cout << "not integral";
    }
    

    will often fail to compile as you will not be able to instantiate X for types other then integral and eventually floating point and user defined classes which have operator int() (operator short() or operator long() will not do).

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

Sidebar

Related Questions

After watching Sussman's lecture http://www.infoq.com/presentations/We-Really-Dont-Know-How-To-Compute , I am inspired to give core.logic and core.match
After watching the webinar, skimming over the BuckyBook PDF, and following the Eclipse RCP
I was watching the following video on codeigniter tv website and wanted to work
I was watching today's Microsoft's Keynote at CES 2012 and noticed the demonstrator searching
After watching the latest hanselminutes on 9 video, I went to our dev environment
I'm watching a file with the following code: [..] FileSystemWatcher watcher = new FileSystemWatcher();
After watching this video , I started thinking about how I could implement something
I was watching the WWDC 2009 Keynote and something someone said about Windows 7/Vista
While I am watching the demo with the following link http://www.jetbrains.com/resharper/demos/presentation/overview/refactorings/Refactor_this_demo.htm I saw this
Watching a tutorial today I came across the following: var q2Var1 = hi there.,

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.