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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:21:08+00:00 2026-05-14T15:21:08+00:00

In < Modern C++ Design >,it introduces a way to check if a type

  • 0

In < Modern C++ Design >,it introduces a way to check if a type fundamental type by introducing the so called type list. but what if I don’t want to include so many loki code and just want a simple function to implement that? What is the simplest way to do that?

  • 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-14T15:21:09+00:00Added an answer on May 14, 2026 at 3:21 pm

    You can use template specialization to get what you want.

    // General template
    template<typename T>
    struct IsFundamentalType { enum { result = false }; };
    
    // Template specializations for each fundamental type
    template<>
    struct IsFundamentalType<char> { enum { result = true }; };
    
    template<>
    struct IsFundamentalType<int> { enum { result = true }; };
    
    template<>
    struct IsFundamentalType<short> { enum { result = true }; };
    
    template<>
    struct IsFundamentalType<float> { enum { result = true }; };
    
    // And so on for other fundamental types ...
    
    class NonFundamentalType
    {
    };
    
    template<typename T>
    void DoSomething(const T& var)
    {
        if(IsFundamentalType<T>::result)
        {
            printf("I'm fundamental type!\n");
        }
        else
        {
            printf("I'm not a fundamental type!\n");
        }
    }
    
    int main()
    {
        int i = 42;
        char c = 42;
        short s = 42;
        float f = 42.0f;
        NonFundamentalType nft;
        DoSomething(i);
        DoSomething(c);
        DoSomething(s);
        DoSomething(f);
        DoSomething(nft);
    }
    

    In this code, if you pass in a type such as int or char, the compiler will use the specialization of IsFundamentalType (given that you’ve defined the specializations for all fundamental types). Otherwise, the compiler will use the general template, as it is the case for the NonFundamentalType class. The important thing is that the specialized ones have a result member defined as true, while the general template also has a result member defined as false. You can then use the result member for the if statement. Optimizing compilers should be able to elide the if statement seeing that the expression reduces to a constant true/false value, so doing something like this should not impose a runtime penalty.

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

Sidebar

Related Questions

I've not read the Modern C++ Design book but have found the idea of
I'm trying to use type traits like in Modern C++ Design using a template
Following techniques from 'Modern C++ Design', I am implementing a persistence library with various
I have just started reading Modern C++ Design Generic programming and Design Patterns Applied
Modern ATL/MFC applications now have access to a new shared pointer class called CAutoPtr,
Modern browsers have multi-tab interface, but JavaScript function window.showModalDialog() creates a modal dialog that
Since many modern browsers support CSS3, I would like to use many CSS 3
Modern C++ Design gives the following example: template <class T> struct EnsureNotNull { static
Has anybody used SmallObjectAllocator from Modern C++ Design by Andrei Alexandrescu in a big
I've read about Small-Object Allocation in Modern C++ Design. Andrei Alexandrescu argues that the

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.