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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:03:30+00:00 2026-05-26T18:03:30+00:00

Possible Duplicate: trailing return type using decltype with a variadic template function I want

  • 0

Possible Duplicate:
trailing return type using decltype with a variadic template function

I want to make a function that sums up several values. If I don’t use a trailing return type then count() uses the type of the first argument as it’s return type. However, when using a trailing return type I am not able to get the code to compile:

#include <iostream>

template<typename T>
inline T sum(T a) {
  return a;
}

template<typename T, typename... Args>
inline auto sum(T a, Args... b) -> decltype(a + sum(b...)) {
  return (a + sum(b...));
}

int main() {
  std::cout << sum(1, 2.5f, 3, 4);
  return 0;
}

The error (GCC) is:

main.cpp: In function ‘int main()’:
main.cpp:16:30: error: no matching function for call to ‘sum(int, float, int, int)’
main.cpp:16:30: note: candidates are:
main.cpp:5:10: note: template<class T> T sum(T)
main.cpp:10:13: note: template<class T, class ... Args> decltype ((a + sum(sum::b ...))) sum(T, Args ...)

How can I get this to work?

  • 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-26T18:03:31+00:00Added an answer on May 26, 2026 at 6:03 pm

    It’s not pretty, but I got it working using a template struct:

    #include <iostream>
    
    template<typename... Args>
    struct sum_helper;
    
    template<typename T>
    struct sum_helper<T> {
      typedef T sum_type;
    };
    
    template<typename T, typename... Args>
    struct sum_helper<T, Args...> {
      typedef decltype(*(T*)0 + *(typename sum_helper<Args...>::sum_type*)0) sum_type;
    };
    
    template<typename T>
    inline T sum(T a) {
      return a;
    }
    
    template<typename T, typename... Args>
    inline typename sum_helper<T, Args...>::sum_type sum(T a, Args... b) {
      return a + sum(b...);
    }
    
    int main()
    {
      std::cout << sum(1, 2, 3) << " "
            << sum(1, 1.1, 1.11) << std::endl;
      return 0;
    }
    

    http://ideone.com/7wYXf

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

Sidebar

Related Questions

Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: Why is my return type meaningless? Hi, I'm confused about a particular
Possible Duplicate: Can we change the device time using an application? is that possible
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: question about leading zeros As in stackoverflow.com/questions/3232534/question-about-leading-zeros . Number of trailing zeros,
Possible Duplicate: How to horizontally center a div? One simple way to make an
Possible Duplicate: Lock the Android device programmatically I want to lock the screen through
Possible Duplicate: Parse string into argv/argc I'm trying to write a fake shell using
Possible Duplicate: Why does “abcd”.StartsWith(“”) return true? Whilst debugging through some code I found

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.