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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:00:01+00:00 2026-06-17T06:00:01+00:00

How do I write a class template that accepts only numeric types ( int

  • 0

How do I write a class template that accepts only numeric types (int, double, float, etc.) as template?

  • 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-06-17T06:00:03+00:00Added an answer on June 17, 2026 at 6:00 am

    You can use the std::is_arithmetic type trait. If you want to only enable instantiation of a class with such a type, use it in conjunction with std::enable_if:

    #include <type_traits>
    
    template<
        typename T, //real type
        typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type
    > struct S{};
    
    int main() {
       S<int> s; //compiles
       S<char*> s; //doesn't compile
    }
    

    For a version of enable_if that’s easier to use, and a free addition of disable_if, I highly recommend reading this wonderful article on the matter.

    In C++, the technique described above has a name called "Substitution Failure Is Not An Error" (most use the acronym SFINAE). You can read more about this C++ technique on wikipedia or cppreference.com.

    As of C++20, concepts make this much easier and don’t spoil the interface:

    #include <concepts>
    
    template<typename T>
    concept arithmetic = std::integral<T> or std::floating_point<T>;
    
    template<typename T>
      requires arithmetic<T>
    struct S{};
    // Shorthand: template<arithmetic T> struct S {};
    

    Do note that there are many user types meant to be used arithmetically as well, though, so a more general concept that covers the operations you’re looking for instead of the types you’re looking for would be preferable in a generic interface.

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

Sidebar

Related Questions

I want to write a math vector template. I have a class which accepts
I am attempting to write a template/class that has a few functions, but I'm
How to write copy constructor for a template class. So that if the template
suppose I write a template class with a template constructor, like that. template<typename T>
I am trying to write a template class in C++ and getting this strange
noob here still experimenting with templates. Trying to write a message processing class template
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I am trying to write class that inherits from FMX TStyledControl. When style is
I want to write a class with three int values in them and manipulate
I have that template class that uses a policy for it's output and another

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.