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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:22:17+00:00 2026-05-28T04:22:17+00:00

Is there a subtle trick for template specialization so that I can apply one

  • 0

Is there a subtle trick for template specialization so that I can apply one specialization to basic POD (when I say basic POD I don’t particularly want struct POD (but I will take that)).

template<typename T>
struct DoStuff
{
    void operator()() { std::cout << "Generic\n";}
};
template<>
struct DoStuff</*SOme Magic*/>
{
    void operator()() { std::cout << "POD Type\n";}
};

Or do I have to write specializations for each of the built in types?

template<typename T>
struct DoStuff
{
    void operator()() { std::cout << "Generic\n";}
};


// Repeat the following template for each of
// unsigned long long, unsigned long, unsigned int, unsigned short, unsigned char
//          long long,          long,          int,          short, signed   char
// long double, double, float, bool
// Did I forget anything?
//
// Is char covered by unsigned/signed char or do I need a specialization for that?
template<>  
struct DoStuff<int>
{
    void operator()() { std::cout << "POD Type\n";}
};

Unit Test.

int main()
{
    DoStuff<int>           intStuff;
    intStuff();            // Print POD Type


    DoStuff<std::string>   strStuff;
    strStuff();            // Print Generic
}
  • 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-28T04:22:17+00:00Added an answer on May 28, 2026 at 4:22 am

    If you really want only fundamental types and not user-defined POD types then the following should work:

    #include <iostream>
    #include <boost/type_traits/integral_constant.hpp>
    #include <boost/type_traits/is_fundamental.hpp>
    #include <boost/type_traits/is_same.hpp>
    
    template<typename T>
    struct non_void_fundamental : boost::integral_constant<
        bool,
        boost::is_fundamental<T>::value && !boost::is_same<T, void>::value
    >
    { };
    
    template<typename T, bool Enable = non_void_fundamental<T>::value>
    struct DoStuff
    {
        void operator ()() { std::cout << "Generic\n"; } const
    };
    
    template<>
    struct DoStuff<T, true>
    {
        void operator ()() { std::cout << "POD Type\n"; } const
    };
    

    If you also want user-defined POD types, then use boost::is_pod<> instead of non_void_fundamental<> (and if you’re using C++11 and doing this for optimization purposes, use std::is_trivially_copyable<> instead).

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

Sidebar

Related Questions

I think there must be something subtle going on here that I don't know
In Ruby 1.8, there are subtle differences between proc/lambda on the one hand, and
In this post , @kbrimington mentions that there is a subtle difference between which
Here's what I'm trying. What I want is the last echo to say one
MyClass.class and MyClass.getClass() both seem to return a java.lang.Class . Is there a subtle
Both ideas sound very similar to me, but there might be subtle differences or
There is a conversion process that is needed when migrating Visual Studio 2005 web
There is a subtle reason why it might not be good: Sometimes, the blame
Is there a better way to write the following so that: a) fewer variables
These two terms have been used frequently in virtualization. Is there a subtle difference?

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.