Search code examples
c++stlvariadic-functionsvariadic-macros

too few arguments for class template "std::pair" : Passing std pair as arguments in varidiac function


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,


Solution

  • 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);