SBRM/RAII 用于 std::va_list/va_start()/va_end 使用 [英] SBRM/RAII for std::va_list/va_start()/va_end use
问题描述
我的代码包含如下片段:
My code contains snippets like these:
std::va_list ap;
va_start(ap, msgfmt);
snprintf_buf buf;
const tchar * msg = buf.print_va_list(msgfmt, ap);
va_end(ap);
它们很短,va_start()
和 va_end()
靠得很近,所以它们不是什么大问题.两者之间调用的异常可能是一个问题(或不是?).
These are short and va_start()
and va_end()
are close together so they are not much of a problem. Exceptions from calls in between the two could be a problem (or not?).
简单测试表明,不允许从没有省略号的函数中调用 va_start()
.调用 va_end()
是否从不同于 va_start()
的函数调用?
Simple test shows that calling va_start()
from a function without ellipsis is not allowed. Is calling va_end()
from a different function than va_start()
was called from allowed or not?
基本上,我很好奇是否可以对这些调用使用 SBRM/RAII 习惯用法,即使有必要手动调用 va_start()
然后传递 的实例std::va_list
到我的 RAII/SBRM 守卫实例?
Basically, I am curious if it is possible to use the SBRM/RAII idiom for these calls, even if it were necessary to call va_start()
manually and then to pass instance of std::va_list
into my RAII/SBRM guard instance?
推荐答案
很遗憾,没有.va_start
和 va_end
的规范要求:
Unfortunately, no. The specification of va_start
and va_end
requires that:
va_start
和 va_copy
宏的每次调用应与 va_end
宏 的相应调用相匹配功能.
Each invocation of the
va_start
andva_copy
macros shall be matched by a corresponding invocation of theva_end
macro in the same function.
因此,va_end
必须在可变参数函数本身中,而不是类析构函数.
Therefore, va_end
must be in the variadic function itself, not a class destructor.
这篇关于SBRM/RAII 用于 std::va_list/va_start()/va_end 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!