I wrote this code in order to use sprintf-style formatting for std::string.
#include <iostream>
#include <string>
#include <vector>
#include <stdarg.h>
std::string formatstr(const std::string &fmt, ...)
{
const char *fmt_s(fmt.c_str());
std::vector<char> buf(256);
va_list args;
va_start(args, &fmt);
auto n = -1;
while ((n = vsnprintf(&buf[0], buf.size() - 1, fmt_s, args)) == -1)
buf.resize(2 * buf.size());
va_end(args);
std::string text(&buf[0]);
return text;
}
int main(int argc, char** argv)
{
std::string s = "Instrument label: %s";
std::cout << formatstr(s, "Frequency Generator") << "\n";
return EXIT_SUCCESS;
}
Output:
Instrument label: Frequency Generator
It works fine in 64-bit builds.
But, as soon as I change the configuration to 32-bit, the VS editor adds the little red squiggly error line under va_start
and complains: expression must be an lvalue or xvalue
Is it an error that my 64-bit build works? Or an error in Visual Studio that it doesn't for 32-bit? Is there some fundamental difference?