Multi-Argument Function ?

How can I make multi-argument function.The least exhaustive way ?

function add for example.

add(2,3) = 5
add(2,3,4) = 9

How can people use as many arguments as they like,without a bunch of work from the programmers ?
Check out the cstdarg header here:

http://www.cplusplus.com/reference/clibrary/cstdarg/
Ask for a container (alternative: iterators)
How ? A Container,ok,but how can you make the function ?
Easy with boost:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <boost/fusion/tuple.hpp>
#include <boost/fusion/include/accumulate.hpp>
template<typename... Args>
int add(Args... args)
{
    return accumulate(boost::fusion::make_tuple(args...), 0, std::plus<int>());
}
int main()
{
    std::cout << add(2,3) << ' ' << add(2,3,4) << '\n';
}


although this hardcodes the return type as int, you may want to throw in a common_type, or make the return type a template parameter.
Last edited on
Cool, I wonder how they're implemented.I can't make one without using a library.
Topic archived. No new replies allowed.