dar*_*une 14 c++ variadic-templates fold-expression c++17
template<class Msg, class... Args>
std::wstring descf(Msg, Args&&... args) {
    std::wostringstream woss;
    owss << Msg << ". " << ... << " " << args << ": '" << args << "' ";//not legal at all
    //or
    owss << Msg << ". " << args[0] << ": '" << args[1] << "'  " << args[2] << ": '" << args[3] << "' "; //... pseudo code, and so on...
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以只使用对列表或类似的东西,但我对如何做到这一点很感兴趣,同时保持函数的语法为:
const auto formatted = descf(L"message", "arg1", arg1, "arg2", arg2);
Run Code Online (Sandbox Code Playgroud)
    使用遵循以下模式的几个辅助函数,这很容易。
void helper() {}
template <class T1, class T2, class ... T>
void helper(T1 t1, T2 t2, T ... t)
{
     do_single_pair(t1, t2);
     helper(t...);
}
Run Code Online (Sandbox Code Playgroud)
这不是折叠表达式,但最终结果是相同的。
您可以使用折叠表达式!它不是最漂亮的*,但比所有提供的非折叠解决方案都要短:
template<class T, class ... Args>
std::wstring descf(T msg, Args&&... args) {
    std::wostringstream owss;
    owss << msg << ". ";
    std::array<const char*, 2> tokens{": '", "' "};
    int alternate = 0;
    ((owss << args << tokens[alternate], alternate = 1 - alternate), ...);
    return owss.str();
}
Run Code Online (Sandbox Code Playgroud)
带有示例输出的演示:https : //godbolt.org/z/Gs8d2x
我们对逗号运算符执行折叠,其中每个操作数是一个args和交替标记的输出,加上转换标记索引(后两个与另一个逗号运算符组合)。
*对于熟悉折叠表达式(和逗号运算符)的读者来说,这可能是“最好的”代码,但对于其他人来说,这完全是胡言乱语,因此请自行判断是否要在代码库中使用它。
我想您可以尝试使用索引和三元运算符。
如下
template <typename ... Args>
std::wstring descf (std::wstring const & Msg, Args && ... args)
 {
   std::wostringstream woss;
   int i = 0;
   ((woss << Msg << ". "), ... ,(woss << args << (++i & 1 ? ": '" : "' ")));
   return woss.str();
 }
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           516 次  |  
        
|   最近记录:  |