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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:54:13+00:00 2026-06-12T03:54:13+00:00

A summation of the Explicit Template Argument Specification template<class T> T max(T t1, T

  • 0

A summation of the Explicit Template Argument Specification

template<class T>
T max(T t1, T t2)
{
   if (t1 > t2)
      return t1;
   return t2;
}
max<double>(120, 14.55);   we explicitly determine the type of T as double 

I understand the part above.

Below , it is a bit different

 template<class T>
T SumOfNumbers(int a, int b)
{
   T t = T(); // ???

   t = T(a)+b;  //??? 

   return t;
}

Which takes two ints, and sums them up. Though, summing them in int itself is appropriate, this function template gives opportunity to calculate the sum (using operator+) in any type as required by caller. For example, the get the result in double, you would call it as:

double nSum;
nSum = SumOfNumbers<double>(120,200);    //  ???

I understand the topic “Explicit Template Argument Specification”. But , here the condition is different , bcs function template’s arguments’ types are already is definite.

I can’t understand the lines before the sign “???” ?

Could you please explain it to me step by step ? What does happen at this line

nSum = SumOfNumbers<double>(120,200); 

Does 120 converted 120.0 namely from int to double ?

What T(a) ? What does it mean?

Reference:
http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1

  • 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-12T03:54:15+00:00Added an answer on June 12, 2026 at 3:54 am
    T t = T();
    

    Initialises t by value-initialisation. For built-in arithmetic types, it is given the value zero; for user-defined types, it is initialised using the default constructor.

    (Pedantically, it’s initialised by copying or moving a value-initialised temporary, so this will fail if no copy or move constructor is available; in practice the copy or move will be elided).

    t = T(a)+b;
    

    Converts a to type T, adds b to that converted value, and assigns the result to t. If T is a built-in type, then T(a) will use a standard conversion or cast; if it’s user-defined, then it will use a constructor of the form T(int).

    There’s no point to the first line, since t is going to be reassigned immediately. The function could be written more clearly as return T(a)+b;

    nSum = SumOfNumbers<double>(120,200);
    

    This instantiates the function template with a return type of double, and calls. The overall effect is the same as nSum = double(120) + 200; or nSum = 220.0.

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

Sidebar

Related Questions

Here is my code (for a neuron in a MLP network): double summation =
Circle Summation: There are N children sitting along a circle, numbered 1,2,...,N clockwise. The
I want to compute a summation to a specified number of terms in c#,
I want to use XSLT to calculate the summation value of amount the input
I need to determine the standard deviation of a set of inputs. This requires
i try this code to summation the pixels coordinates (x,y) in the image this
I have seen code for summation of an array using threads. In this code
How do you perform such summation in R? sum_{i=1}^3 (x^2) i=1 is lower bound
On the Wikipedia page about summation it says that the equivalent operation in Haskell
How do you do a variable summation in the constraint programming language ECLiPSe (

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.