I
I
IliaNeverov2021-07-31 17:57:26
C++ / C#
IliaNeverov, 2021-07-31 17:57:26

How to make a function accept an unlimited number of arguments of different types?

How to make a function accept an unlimited number of arguments of different types?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evgeny Shatunov, 2021-07-31
@IliaNeverov

In C++, it is much more difficult to write a function that takes a variable number of arguments of the same type than it is to write different types.
The problem is solved with the help of the so-called. variable template parameters [ ? ].

template< typename... TArguments >
void Foo( const TArguments&... arguments )
{
  static_assert( sizeof...( TArguments ) > 0, "This function shall not be called without arguments." );
}

When working with a variable number of template parameters, in view of its syntax, it is especially necessary to take into account the functionality of the ideal passing [ ? ], and the link collapse rule [ ? ].

K
Kirill Gusarev, 2021-07-31
@kaka888

Unlimited number of arguments - you can simply take a pointer to the beginning of the list as a single argument, for example. And the list can be of any length.
In order for a function to work with different inputs, use templates (function templates): Wikipedia

S
Sergey Karbivnichy, 2021-07-31
@hottabxp

Write a function that will accept unlimited...

1
15432, 2021-07-31
@15432

By the type of printf (), you can, through va_list, va_start

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question