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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:36:57+00:00 2026-05-10T16:36:57+00:00

I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function. How do

  • 0

I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function.

How do I retrict the template function to only use numbers? (int, double etc.)

#include <vector> #include <iostream>  using namespace std;      template <typename T> T sum(vector<T>& a) {     T result = 0;     int size = a.size();     for(int i = 0; i < size; i++)     {         result += a[i];     }      return result; }  int main() {     vector<int> int_values;     int_values.push_back(2);     int_values.push_back(3);     cout << 'Integer: ' << sum(int_values) << endl;      vector<double> double_values;     double_values.push_back(1.5);     double_values.push_back(2.1);     cout << 'Double: ' << sum(double_values);      return 0; } 
  • 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. 2026-05-10T16:36:57+00:00Added an answer on May 10, 2026 at 4:36 pm

    The only way to restrict a template is to make it so that it uses something from the types that you want, that other types don’t have.

    So, you construct with an int, use + and +=, call a copy constructor, etc.

    Any type that has all of these will work with your function — so, if I create a new type that has these features, your function will work on it — which is great, isn’t it?

    If you want to restrict it more, use more functions that only are defined for the type you want.

    Another way to implement this is by creating a traits template — something like this

    template<class T> SumTraits { public:   const static bool canUseSum = false; } 

    And then specialize it for the classes you want to be ok:

    template<> class SumTraits<int> {   public:     const static bool canUseSum = true; }; 

    Then in your code, you can write

    if (!SumTraits<T>::canUseSum) {    // throw something here } 

    edit: as mentioned in the comments, you can use BOOST_STATIC_ASSERT to make it a compile-time check instead of a run-time one

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

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer This is my implementation of it from my bare-bones framework… May 11, 2026 at 11:40 am
  • added an answer The only way I can think of would be using… May 11, 2026 at 11:40 am
  • added an answer Well I would agree that it doesn't server much purpose,… May 11, 2026 at 11:40 am

Related Questions

I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function. How do
I wrote a simple Windows Forms program in C#. I want to be able
I'm trying to write a simple program in VC++ which will just initialize the
I'm just starting out writing trying to write a simple program in C and
I wrote a small sample of code in C# to capture selected text from
I wrote a simple batch file as a PowerShell script, and I am getting
I wrote a simple tool to generate a DBUnit XML dataset using queries that
I wrote a simple web service in C# using SharpDevelop (which I just got
I wrote a simple javascript image rotator which picks a random image on each
I have a simple Google App Engine app, that I wrote using ordinary strings.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.