如何使用折叠表达式在 (cout << ... << args) 中插入空格?
How to Insert spaces in (cout << ... << args) using fold expressions?
给出
template<typename ...Types>
void print(Types&& ...args) {
(cout << ... << args);
}
// ....
print(1, 2, 3, 4); // prints 1234
如何添加空格得到1 2 3 4
?
更新:
正确答案:
((std::cout << args << ' ') , ...);
通常的解决方法是折叠逗号运算符,但简单的方法会留下尾随 space:
((std::cout << args << ' '), ...);
更改它以避免尾随 space 留作 reader 的练习。
给出
template<typename ...Types>
void print(Types&& ...args) {
(cout << ... << args);
}
// ....
print(1, 2, 3, 4); // prints 1234
如何添加空格得到1 2 3 4
?
更新:
正确答案:
((std::cout << args << ' ') , ...);
通常的解决方法是折叠逗号运算符,但简单的方法会留下尾随 space:
((std::cout << args << ' '), ...);
更改它以避免尾随 space 留作 reader 的练习。