4

Is there any way at all for this code to compile and work as intended without resorting to va_list stuff ?

#include <iostream>

void fct(void)
{
    std::cout << std::endl;
}

void fct(int index, int indexes...)
{
    std::cout << index << ' ';
    fct(indexes); //or fct(indexes...); ?
}

int main(void)
{
    fct(1, 2, 3, 4, 5, 6, 7);
    return 0;
}
max66
  • 65,235
  • 10
  • 71
  • 111
Pippin
  • 323
  • 1
  • 9
  • Have you considered making `fct` a variadic template function? – cantordust May 12 '18 at 07:40
  • How would that solve my problem? Would this not uselessly complicate things? What I'm trying to achieve seems very simple so far. – Pippin May 12 '18 at 08:29
  • The truth is, I can't use variadic templates here because I already do. The above code is very simplified to get to the core of my problem, but the real code I'm working on has an std::size_t variadic template. I would like to solve this problem without templates. – Pippin May 12 '18 at 09:34
  • 1
    I'm not sure I follow. Why can't you make this a template? – cantordust May 12 '18 at 10:27
  • @cantordust I've presented my problem here: https://stackoverflow.com/questions/50307095/passing-variadic-parameters-in-an-already-template-variadic-function – Pippin May 12 '18 at 14:08

2 Answers2

2

I suspect you have misunderstood the meaning of the signature

void fct (int index, int indexes...)

I suspect you think that fct() expect a int single value (index) and a variadic list of int's (indexex...) with C++11 style of parameter pack expansion.

No: it's the same as

void fct (int index, int indexes, ...)

so two int single values and a C-style of optional argument that you can use only through va_list stuff.

If you don't believe it, try calling fct() with only an integer argument

fct(1);

You should obtain an error of type "error: no matching function for call to 'fct'" with a note of type "note: candidate function not viable: requires at least 2 arguments, but 1 was provided" regarding the variadic version of fct().

If you want receive a variadic list of parameters and recursively pass the to the same function, you can use the template variadic way.

By example

template <typename ... Ts>
void fct(int index, Ts ... indexes)
{
    std::cout << index << ' ';
    fct(indexes...);
}
max66
  • 65,235
  • 10
  • 71
  • 111
  • And maybe throw in a `void fct(){ std::cout << std::endl; }` to print the `endl` – cantordust May 12 '18 at 10:34
  • Thank you for your reply. In other words, there is absolutely no way to solve this issue without templates and `va_list` :\ – Pippin May 12 '18 at 10:45
  • 1
    @Pippin - No, as far I know. Or better: the way (starting from C++11) is the variadic template one. I don't understand what's wrong, for you, with the variadic template way. – max66 May 12 '18 at 11:38
  • @max66 I've presented my problem here: https://stackoverflow.com/questions/50307095/passing-variadic-parameters-in-an-already-template-variadic-function – Pippin May 12 '18 at 14:08
  • @Pippin - as you can see in my answer in the new question, the use of variadic templates is extremely simple. – max66 May 12 '18 at 16:15
0

If you really dislike the idea of a template, I guess you could cheat a bit like this:

#include <iostream>
#include <vector>

void fct(std::vector<int>&& _indices)
{
    for (auto&& i : _indices)
    {
        std::cout << i << ' ';
    }
    std::cout << std::endl;
}

int main(void)
{
    fct({1, 2, 3, 4, 5, 6, 7}); // Note the curly braces
    return 0;
}
cantordust
  • 1,542
  • 13
  • 17