带有可变参数模板参数的boost :: format

无效指针

假设我有一个printf利用完美转发的类似函数(用于记录):

template<typename... Arguments>
void awesome_printf(std::string const& fmt, Arguments&&... args)
{
    boost::format f(fmt);
    f % /* How to specify `args` here? */;
    BlackBoxLogFunction(boost::str(f).c_str());
}

(我没有编译它,但是我的真实功能遵循了该准则)

如何将可变参数传递给boost :: format变量f

无效指针

我做了一些谷歌搜索,发现了一个有趣的解决方案:

#include <iostream>
#include <boost/format.hpp>

template<typename... Arguments>
void format_vargs(std::string const& fmt, Arguments&&... args)
{
    boost::format f(fmt);
    int unroll[] {0, (f % std::forward<Arguments>(args), 0)...};
    static_cast<void>(unroll);

    std::cout << boost::str(f);
}

int main()
{
    format_vargs("%s %d %d", "Test", 1, 2);
}

我不知道这是否是推荐的解决方案,但它似乎有效。我不喜欢这种骇人听闻的static_cast用法,这种用法似乎有必要使GCC上未使用的变量警告静音。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章