Va_copy - портирование на визуальный С++?
Предыдущий вопрос показал хороший способ печати в строке. Ответ включал va_copy:
std::string format (const char *fmt, ...);
{
va_list ap;
va_start (ap, fmt);
std::string buf = vformat (fmt, ap);
va_end (ap);
return buf;
}
std::string vformat (const char *fmt, va_list ap)
{
// Allocate a buffer on the stack that big enough for us almost
// all the time.
s ize_t size = 1024;
char buf[size];
// Try to vsnprintf into our buffer.
va_list apcopy;
va_copy (apcopy, ap);
int needed = vsnprintf (&buf[0], size, fmt, ap);
if (needed <= size) {
// It fit fine the first time, we're done.
return std::string (&buf[0]);
} else {
// vsnprintf reported that it wanted to write more characters
// than we allotted. So do a malloc of the right size and try again.
// This doesn't happen very often if we chose our initial size
// well.
std::vector <char> buf;
size = needed;
buf.resize (size);
needed = vsnprintf (&buf[0], size, fmt, apcopy);
return std::string (&buf[0]);
}
}
Проблема, с которой я столкнулась, заключается в том, что вышеуказанный код не переносит на Visual С++, поскольку он не предоставляет va_copy (или даже __va_copy). Итак, кто-нибудь знает, как безопасно переносить вышеуказанный код? Предположительно, мне нужно сделать копию va_copy, потому что vsnprintf деструктивно изменяет пройденный va_list.
Ответы
Ответ 1
Вы должны уйти от выполнения обычного задания:
va_list apcopy = ap;
Это технически не портативное и undefined поведение, но оно будет работать с большинством компиляторов и архитектур. В соглашении о вызове x86 va_list
являются просто указателями в стеке и безопасны для копирования.
Ответ 2
Для Windows вы можете просто определить va_copy самостоятельно:
#define va_copy(dest, src) (dest = src)
Ответ 3
Одна вещь, которую вы можете сделать, - это если вам не нужна функция vformat()
, переместите ее реализацию в функцию format()
(untested):
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <vector>
std::string format(const char *fmt, ...)
{
va_list ap;
enum {size = 1024};
// if you want a buffer on the stack for the 99% of the time case
// for efficiency or whatever), I suggest something like
// STLSoft auto_buffer<> template.
//
// http://www.synesis.com.au/software/stlsoft/doc-1.9/classstlsoft_1_1auto__buffer.html
//
std::vector<char> buf( size);
//
// where you get a proper vsnprintf() for MSVC is another problem
// maybe look at http://www.jhweiss.de/software/snprintf.html
//
// note that vsnprintf() might use the passed ap with the
// va_arg() macro. This would invalidate ap here, so we
// we va_end() it here, and have to redo the va_start()
// if we want to use it again. From the C standard:
//
// The object ap may be passed as an argument to
// another function; if that function invokes the
// va_arg macro with parameter ap, the value of ap
// in the calling function is indeterminate and
// shall be passed to the va_end macro prior to
// any further reference to ap.
//
// Thanks to Rob Kennedy for pointing that out.
//
va_start (ap, fmt);
int needed = vsnprintf (&buf[0], buf.size(), fmt, ap);
va_end( ap);
if (needed >= size) {
// vsnprintf reported that it wanted to write more characters
// than we allotted. So do a malloc of the right size and try again.
// This doesn't happen very often if we chose our initial size
// well.
buf.resize( needed + 1);
va_start (ap, fmt);
needed = vsnprintf (&buf[0], buf.size(), fmt, ap);
va_end( ap);
assert( needed < buf.size());
}
return std::string( &buf[0]);
}
Ответ 4
va_copy()
поддерживается напрямую, начиная с Visual Studio 2013. Поэтому, если вы можете полагаться на то, что это доступно, вам ничего не нужно делать.