SBRM/RAII for std::va_list/va_start()/va_end use

穿精又带淫゛_ 提交于 2020-01-01 05:20:11

问题


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);

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?).

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?

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?


回答1:


Unfortunately, no. The specification of va_start and va_end requires that:

Each invocation of the va_start and va_copy macros shall be matched by a corresponding invocation of the va_end macro in the same function.

Therefore, va_end must be in the variadic function itself, not a class destructor.




回答2:


One of possible implementations assumes std::va_list = char* and va_end() is just setting that pointer to null. Of cause, it can be called outside of function. But I'm not sure, that it will works similar on other platforms.

Better to wrap this functions with a class.



来源:https://stackoverflow.com/questions/12141795/sbrm-raii-for-stdva-list-va-start-va-end-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!