利用可变模板将不定入参连接为字符串返回,可用于构建一个入参不定日志系统,用于代替C语言的可变参数。
c++版本不小于c++17, c++11需要使用模板递归
代码如下:
// 模板函数需放在头文件中
template<typename... Args>
inline std::string LINK_TO_STRING(const Args&... args)
{
std::stringstream result;
((result << args), ...); // 折叠表达式 (C++17)
return result.str();
}