1

I have to pass std::pair of std::string to a variadic function. std::pair shows error too few arguments for class template "std::pair" when trying to access std::pair using va_arg macro.

#include <stdarg.h>
#include <tuple>
#include <string>
using std::pair;
using std::string;

bool EncodeJSonData(pair<string,string> inbulkData ...)
{     
    va_list args; 
    va_start(args, inbulkData);
    int count = 5;
    while(count--)
    { 
        pair<string,string> bulkData;
        bulkData = va_arg(args, pair<string,string>);  //here is the error      
    }
    va_end(args); 

    return true;
}

What is missing here,

Akhil V Suku
  • 870
  • 2
  • 13
  • 34
  • Have you considered passing a container (e.g., vector) of pairs? A C variadic function will let the user pass whatever type they want without the compiler catching it. In addition, it's not guaranteed to work for C++ strings (including pairs of such) and most other interesting classes. – chris May 04 '18 at 06:07
  • 3
    Please don't use variadic functions in C++. Instead, use variadic templates which are at least type safe. – rubenvb May 04 '18 at 06:09
  • Include all the headers required. – Jive Dadson May 04 '18 at 06:12
  • How is that supposed to terminate? Read this: [MCVE] – Jive Dadson May 04 '18 at 06:23
  • There is no portable way to use varargs to pass `std::pair`. The type is non-trivial. Vararg is a relic of the past. Use variadic templates. – Jive Dadson May 04 '18 at 06:40

1 Answers1

2

va_arg is a macro, and character like the ',' may cause the macro parsing failed

So the solution is typedef the pair<string,string>:

    typedef pair<string, string> StrStrPair;
    StrStrPair bulkData;
    bulkData = va_arg(args, StrStrPair);
Monkey Shen
  • 199
  • 1
  • 6