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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:12:49+00:00 2026-06-08T01:12:49+00:00

Possible Duplicate: Variable number of arguments in C++? May I not set the number

  • 0

Possible Duplicate:
Variable number of arguments in C++?

May I not set the number of arguments of a function with variable number of arguments? As an example: can the following interface be implemented?

int sum(...) { ... }

sum(1, 2, 3, 4); // return 10
  • 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-08T01:12:51+00:00Added an answer on June 8, 2026 at 1:12 am

    Conventional variadic functions are messy and not type-safe, but in C++11 you can do this cleanly using variadic templates and (compile-time) recursion:

    // Base case for recursion
    template <typename T>
    inline T sum(T n) {
      return n;
    }
    
    // Recursive case
    template <typename T, typename... Args>
    inline T sum(T n, Args... args) {
      return n + sum(args...);
    }
    

    Since it’s a template, this’ll work for any types that have an operator+ defined:

    std::cout << sum(1, 2, 3) << std::endl;  // Prints 6
    std::cout << sum(3.14, 2.72) << std::endl;  // Prints 5.86
    

    However, because the return type of the recursive template function is taken from the first argument only, you can get suprising results if you mix different argument types in one call: sum(2.5, 2) returns 4.5 as expected, but sum(2, 2.5) returns 2 because the return type is int, not double. If you want to be fancy, you can use the new alternative function syntax to specify that the return type is whatever the natural type of n + sum(args...) would be:

    // Recursive case
    template <typename T, typename... Args>
    inline auto sum(T n, Args... args) -> decltype(n + sum(args...)) {
      return n + sum(args...);
    }
    

    Now sum(2.5, 2) and sum(2, 2.5) both return 4.5.

    If your actual logic is more complex than summation, and you don’t want it inlined, you can use the inline template functions to put all the values into some sort of container (such as a std::vector or std::array) and pass that into the non-inline function to do the real work at the end.

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

Sidebar

Related Questions

Possible Duplicate: C/C++: Passing variable number of arguments around Imagine I have a function
Possible Duplicate: How can I code a C# function to accept a variable number
Possible Duplicate: C/C++: How to make a variadic macro (variable number of arguments) Just
Possible Duplicate: C++ variable types limits I have a defined type that may not
Possible Duplicate: C/C++: Passing variable number of arguments around I need to put all
Possible Duplicate: Is it possible to send a variable number of arguments to a
Possible Duplicate: How would you set a variable to the largest number possible in
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: How to get a variable name as a string in PHP? Example:
Possible Duplicate: Pointer to local variable Can a local variable's memory be accessed outside

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.