4

Can I overload my function to do something with a lot of arguments as in JavaScript. For example:

function f()
{
  alert(arguments[0]);
}

f(4); // will alert 4 

Can I do the same thing in C++ ?

2
  • 1
    So you want to call your function with a variable number of arguments? If that is the case you can always use variadic functions but like this page says en.cppreference.com/w/cpp/utility/variadic std::initializer_list or variadic templates are type safe and easier to use. Commented May 2, 2013 at 11:57
  • Sorry, my example had an extra colon in it which kept it from compiling correctly. See my new edit please along with the demo. Commented May 2, 2013 at 19:34

1 Answer 1

9

You can use variadic template arguments and tuples:

#include <tuple>
#include <iostream>

template <class... Args>
void f(Args&&... args)
{
    auto arguments = std::make_tuple(std::forward<Args>(args)...);
    std::cout << std::get<0>(arguments);
}

void f() {} // for 0 arguments

int main()
{
    f(2, 4, 6, 8);
}

Live Demo

For bounds checking, try the following:

#include <tuple>
#include <iostream>

template <class... T>
struct input
{
    std::tuple<T...> var;
    input(T&&... t) : var(std::forward<T>(t)...) {}

    template <std::size_t N, bool in_range = 0 <= N && N < std::tuple_size<decltype(var)>::value>
    auto get() -> typename std::tuple_element<in_range ? N : 0, std::tuple<T...>>::type
    {
        return std::get<in_range ? N : 0>(var);
    }
};

template <class... Args>
void f(Args&&... args)
{
    auto arguments = input<Args...>(std::forward<Args>(args)...);

    std::cout << arguments.template get<9>();
}

void f() {} // for 0 arguments

int main()
{
    f(2, 4, 6, 8);
}

Update: If you need the first argument then you simply want a function which exposes that first argument by separating it from the parameter pack:

template<class Head, class... Tail>
void foo(Head&& head, Tail&&... tail);

If this is not satisfactory (i.e you want to get the nth-argument), you can unpack the arguments into a std::tuple<> and retrieve an element with std::get<>:

template<class... Args>
void foo(Args&&... args)
{
    auto t = std::forward_as_tuple(std::forward<Args>(args)...);
    print(std::get<5>(t));
}
Sign up to request clarification or add additional context in comments.

11 Comments

@shafikYaghmour Your question is a little vague. Can you narrow it down to a specific scenario?
@ShafikYaghmour You'll need to use some form of looping to get all N elements. You can use a range-based for to do this: for (auto a : tup) print( a ). You can also use compile-time constants through template arguments.
Is there a way to force args to be a single specific type? (Without having to abuse defaults like so: template<class T = int, T... args) void f(T&&... args))
@Pharap You can use SFINAE: template<class T, class...> using first = T; template<class... Args> first<void, typename std::enable_if<std::is_same<Args, int>::value>::type...> f(Args&&...);. Here's an example.
Thank you 1234567890, I was unable to find a decent answer for that anywhere. I was probably searching the wrong terms admittedly. SFINAE is truly the answer to all life's mysteries.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.