This is driving me insane. I just want to send a variadic list of pairs of strings into a template function that replaces words in a string, and the function works, but only with one parameter, not variadic.
This works fine
using std::string;
using spair = std::pair<string, string>;
string replace_all(string& str, const spair& sp)
{
string::size_type pos = 0;
string::size_type rmsize = std::get<0>(sp).size();
while((pos = str.find(std::get<0>(sp), pos)) != string::npos)
{
str.replace(pos, rmsize, std::get<1>(sp));
pos++;
}
return str;
}
string pattern = "My name is {{first_name}} {{last_name}} and I live in {{location}}";
string t = replace_all( pattern, {"{{first_name}}","Homer"} );
/*************************/
But this will not work
template<typename T=spair, typename ...Args>
string replace_all(string& s, const T& arg1, Args const&... args )
{
s = replace_all(s, arg1);
s = replace_all(s, args...);
return s;
}
string v = replace_all( pattern,
{"{{first_name}}","Homer"},
{"{{last_name}}","Simpson"},
{"{{location}}","Springfield"}
);
variadic.cpp:72:8: error: no matching function for call to **‘replace_all(std::string&, <brace-enclosed initializer list>, <brace-enclosed initializer list>, <brace-enclosed initializer list>)’