0

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"

2
  • 1
    Take an initializer list. Commented Sep 4, 2013 at 22:24
  • 1
    You can't pass non-POD values as variable function arguments. Commented Sep 4, 2013 at 22:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I had tried the last variant, but I have syntax error... "shold be ',' before ... in template<typename T1, typename T2, typename ...Rest>", and "synax error: identificator Rest" in second line. I use MS Visual Studio 2012
Suddenly, so I will find another way. thanx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.