Jan 5, 2012 at 7:02pm
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 ?
Jan 5, 2012 at 7:07pm
Ask for a container (alternative: iterators)
Jan 5, 2012 at 7:08pm
How ? A Container,ok,but how can you make the function ?
Jan 5, 2012 at 7:13pm
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 Jan 5, 2012 at 7:17pm
Jan 5, 2012 at 7:26pm
Cool, I wonder how they're implemented.I can't make one without using a library.