sha*_*oth 2 c++ variadic-functions visual-c++
假设我有两个用于调试输出的C++函数:
void Trace( const wchar_t* format, ... )
{
va_list args;
va_start( args, format );
VarArgTrace( format, args );
va_end( args );
}
void VarArgTrace( const wchar_t* format, va_list args )
{
WCHAR buffer[1024];
//use ::_vsnwprintf_s to format the string
::OutputDebugStringW( buffer );
}
Run Code Online (Sandbox Code Playgroud)
以上使用Win32 OutputDebugStringW(),但它并不重要.现在我想优化格式化,以便在没有调试器附加格式化时(我测量 - 加速很重要):
void Trace( const wchar_t* format, ... )
{
if( !IsDebuggerPresent() ) {
return;
}
//proceed as previously
va_list args;
.....
}
Run Code Online (Sandbox Code Playgroud)
将我提前IsDebuggerPresent()返回一次返回null 的事实会影响除格式化之外的所有内容吗?
我的意思是我不再打电话va_start和va_end-这还重要?会跳过va_start并va_end导致任何意外的行为改变吗?
不,没有义务在varargs函数中使用va_start.
如果你不使用va_start,则不能使用va_end; 如果你使用va_start,你应该使用va_end,无论函数如何返回.