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

前端 未结 2 675
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-15 15:42

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


        
相关标签:
2条回答
  • 2021-02-15 16:09

    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.

    0 讨论(0)
  • 2021-02-15 16:25

    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.

    0 讨论(0)
提交回复
热议问题