user1626487
user1626487

Reputation: 1

va_arg error in unknow argument quantity function

Help me please! I am trying to make a function, which takes different quantity of parameters, but all parameter have same type std::pair

Here a code:

void pro(std::pair<int, int*> p, ...)

{
va_list uk_arg;
va_start(uk_arg,p);
std::pair<int,int*> l;
while((l = va_arg(uk_arg,std::pair<int,int*>))!=-1)
{
  show(l.first);
  show(*l.second);
}
  va_end(uk_arg);
}

But this code is not working. I'm receiving an error like "not enought arguments for class template std::pair"

Upvotes: 0

Views: 249

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477512

Use a templated overload:

void pro() { }

template <typename P, typename ...Rest>
void pro(P const & p, Rest const &... rest)
{
    show(p.first);
    show(*p.second);

    pro(rest...);
}

You can add a varying amount of checks that your arguments are pairs; it all depends on what you need. This should do as a first try, though. If you want an earlier compiler error the case of misuse, you could write:

template <typename T1, typename T2, typename ...Rest>
void pro(std::pair<T1, T2> const & p, Rest const &... rest)

Even more advanced versions would use SFINAE to disable any overload for which not all arguments are pairs.

Upvotes: 1

Related Questions