我有这样的 C++14 代码。
我正在更新到 C++17。有没有办法将其重写为折叠表达式?
namespace cat_impl_{
inline size_t catSize(std::string_view const first) {
return first.size();
}
template<typename... Args>
size_t catSize(std::string_view const first, Args &&... args) {
return catSize(first) + catSize(std::forward<Args>(args)...);
}
inline void cat(std::string &s, std::string_view const first) {
s.append(first.data(), first.size());
}
template<typename... Args>
void cat(std::string &s, std::string_view const first, Args &&... args) {
cat(s, first);
cat(s, std::forward<Args>(args)...);
}
}
template<typename... Args>
std::string concatenate(Args &&... args){
// super cheap concatenation,
// with single allocation
using namespace cat_impl_;
size_t const reserve_size = catSize(std::forward<Args>(args)...);
std::string s;
s.reserve(reserve_size);
cat(s, std::forward<Args>(args)...);
return s;
}