Modified number of parameters
-
The variable number of parameters of one type should be transferred. The problem is that the type itself is a class with a variable number of parameters. That's what class looks like:
template <typename... Types, int... Int_pack> class MyType<std::tuple<Types...>, std::tuple<Int_pack...>> { //... };
It is necessary to declare that function.
template<...> void foo(...)
so that it can accept the variable number of parameters of the typeMyType
♪ Trying to do something like:template <typename... Types, int... Int_pack> void foo(const MyType<std::tuple<Types...>, std::tuple<Int_pack...>>& args_pack...) { //... }
It's the only option that doesn't make any mistakes, but it ends only with the first parameter. I tried to use three templates, but I couldn't make it. Tell me how to do the right thing.
-
- I don't understand what you're saying.
class MyType<std::tuple<Types...>, std::tuple<Int_pack...>>
What is it? Is this a tricky performance? In any case, in this particular form, this code collector does not understand. You're doing something wrong. Explain the task more clearly.
- I'll try to answer your original question.
In order to give the variable number of Type T parameters to the function, the following can be done:
- http://en.cppreference.com/w/cpp/utility/variadic - I don't recommend that.
- A suit that accepts any container-like object
For example:
template <typename T, typename Container = std::vector<T>> void foo(const Container& args) { // Container type has to be iterable // i.e. has begin() and end() methods or std::begin() and std::end() can be applied // arg is some container-like object (variable length) of objects of type T. for (const auto& sub_arg : arg) { //sub_arg is an element of type const T& } }
Now you can do this:
foo<int>({ 1,2,3,4,5,6,7,8,9,10 });
or so:
std::set<int> a{ 1,2,3,4,5,6,7,8,9 }; foo<decltype(a)::value_type>(a);
Even so:
int b[] = { 1,2,3,4,5,6,7,8,9,10 }; foo<int>(b);