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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:24:11+00:00 2026-05-16T23:24:11+00:00

I want to write a simple adder (for giggles) that adds up every argument

  • 0

I want to write a simple adder (for giggles) that adds up every argument and returns a sum with appropriate type.
Currently, I’ve got this:

#include <iostream>
using namespace std;

template <class T>
T sum(const T& in)
{
   return in;
}

template <class T, class... P>
auto sum(const T& t, const P&... p) -> decltype(t + sum(p...))
{
   return t + sum(p...);
}

int main()
{
   cout << sum(5, 10.0, 22.2) << endl;
}

On GCC 4.5.1 this seems to work just fine for 2 arguments e.g. sum(2, 5.5) returns with 7.5. However, with more arguments than this, I get errors that sum() is simply not defined yet. If I declare sum() like this however:

template <class T, class P...>
T sum(const T& t, const P&... p);

Then it works for any number of arguments, but sum(2, 5.5) would return integer 7, which is not what I would expect.
With more than two arguments I assume that decltype() would have to do some sort of recursion to be able to deduce the type of t + sum(p…). Is this legal C++0x? or does decltype() only work with non-variadic declarations? If that is the case, how would you write such a function?

  • 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-16T23:24:12+00:00Added an answer on May 16, 2026 at 11:24 pm

    I think the problem is that the variadic function template is only considered declared after you specified its return type so that sum in decltype can never refer to the variadic function template itself. But I’m not sure whether this is a GCC bug or C++0x simply doesn’t allow this. My guess is that C++0x doesn’t allow a “recursive” call in the ->decltype(expr) part.

    As a workaround we can avoid this “recursive” call in ->decltype(expr) with a custom traits class:

    #include <iostream>
    #include <type_traits>
    using namespace std;
    
    template<class T> typename std::add_rvalue_reference<T>::type val();
    
    template<class T> struct id{typedef T type;};
    
    template<class T, class... P> struct sum_type;
    template<class T> struct sum_type<T> : id<T> {};
    template<class T, class U, class... P> struct sum_type<T,U,P...>
    : sum_type< decltype( val<const T&>() + val<const U&>() ), P... > {};
    

    This way, we can replace decltype in your program with typename sum_type<T,P...>::type and it will compile.

    Edit: Since this actually returns decltype((a+b)+c) instead of decltype(a+(b+c)) which would be closer to how you use addition, you could replace the last specialization with this:

    template<class T, class U, class... P> struct sum_type<T,U,P...>
    : id<decltype(
          val<T>()
        + val<typename sum_type<U,P...>::type>()
    )>{};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time that I want to do a Layout, I'm getting a black layout
I want to use a temp directory that will be unique to this build.
I want to have generalised email templates. Currently I have multiple email templates with
I want the messagebox to only show if the number is equal to 0.
I want to get the header of a selected tab-item of a tab-control and
If all tables I want to delete from have the column gamer_id can i
I'm working on a project in C# w/ XNA, and I want to reorganize
I'm trying to build a Chrome browser extension, that should enhance the way the
I'm in the process of porting some code from Linux to Mac OS X.
I am using a 3rd-party rotator object, which is providing a smooth, random rotation

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.