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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:16:51+00:00 2026-06-01T10:16:51+00:00

What is the form (if there is one) to write template functions, where arguments

  • 0

What is the form (if there is one) to write template functions, where arguments are templated containers?

For example I want to write a generic sum, which will work on any container which can be iterated over. Given the code below I have to write for example sum<int>(myInts). I would prefer to just write sum(myInts) and the type to be inferred from the type which myInts contains.

/**
 @brief  Summation for iterable containers of numerical type
 @tparam cN                         Numerical type (that can be summed)
 @param[in]  container              container containing the values, e.g. a vector of doubles
 @param[out] total                  The sum (i.e. total)
 */
template<typename N, typename cN>
N sum(cN container) {
    N total;
    for (N& value : container) {
        total += value;
    }
    return total;
}
  • 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-01T10:16:52+00:00Added an answer on June 1, 2026 at 10:16 am

    This, even if cumbersome, can do the trick in C++11:

    template <typename C>
    auto sum( C const & container ) 
         -> std::decay<decltype( *std::begin(container) )>::type
    

    Another option is just using the same structure that accumulate does: have the caller pass an extra argument with the initial value and use that to control the result of the expression:

    template<typename N, typename cN>
    N sum(cN container, N initial_value = N() )
    

    (By providing a default value, the user can decide to call it with a value or else provide the template argument –but this requires the type N to be default constructible)

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

Sidebar

Related Questions

There are 3+ apps which collect parameters in the form of one string, like
There is one form, inside the form there are many checkboxes. Two operations can
I have a windows forms application in .NET 3.5. There's one form with 20
I have a form where there are 6 items, each of which can be
I am working in a form in which there are check boxes now i
I'm developing a SWT app and in one particular form there are 14 pairs
Is there an xml or some form of a text standard that one can
Inside my view.py, I have two functions, one that processes input from a form
I have an ASP.NET form with three text inputs, one each for Work Phone,
In VB.net 3.5 SP1 I have a Windows Form with three DataGridView controls. One

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.