简体   繁体   English

用字符串及其大小折叠表达式

[英]Fold expression with strings and their size

I have C++14 code like this.我有这样的 C++14 代码。

I am updating to C++17.我正在更新到 C++17。 Is there a way this to be rewritten as fold expression?有没有办法将其重写为折叠表达式?

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;
}

Yes是的

template <typename... Args>
std::size_t catSize (Args &&... args) {
    return (... + std::forward<Args>(args).size());
}

and

template <typename... Args>
void cat (std::string &s, Args ... args) {
    (s.append(args.data(), args.size()), ...);
}

or also (more generic, not only for std::string_view )或者也(更通用,不仅适用于std::string_view

template <typename ... Args>
void cat (std::string &s, Args && ... args) {
    (s += std::forward<Args>(args), ...);
}

Or, maybe, you can avoid at all cat() and catSize() and simply write something as或者,也许,你可以完全避免cat()catSize()并简单地写一些东西

template <typename... Args>
std::string concatenate (Args &&... args) {

    std::string s;

    s.reserve((args.size() + ...));

    return (s += std::forward<Args>(args), ...);
}

Off Topic: avoid a double std::forward , for the same object, in a function (see double std::forward over your args in your original concatenate() ).题外话:避免双std::forward ,对于相同的 object,在 function (请参阅双std::forward在您的原始concatenate()中的args )。

This is what I come up with:这就是我想出的:

#include <string>
#include <string_view>

#include <iostream>

template<typename... Args>
std::string concatenate(Args &&... args){
    static_assert((std::is_constructible_v<std::string_view, Args&&> && ...));

    // super cheap concatenation,
    // with single allocation

    size_t const reserve_size = (std::string_view{ args }.size() + ...);

    std::string s;

    s.reserve(reserve_size);

    (s.append(std::forward<Args>(args)), ...);

    return s;
}

template<typename... Args>
std::string const &concatenate(std::string &s, Args &&... args){
    static_assert((std::is_constructible_v<std::string_view, Args&&> && ...));

    // super cheap concatenation,
    // sometimes without allocation

    size_t const reserve_size = (std::string_view{ args }.size() + ...);

    s.clear();

    // reserve() will shrink capacity
    if (reserve_size > s.capacity())
        s.reserve(reserve_size);

    (s.append(std::forward<Args>(args)), ...);

    return s;
}

int main(){
    auto s = concatenate(std::string("Hello"), std::string_view(" "), "world", "!");

    std::cout << s << '\n';
}

I don;t think I need std::forward, since std::string::append supports std::string , const char * and std::string_view anyway.我不认为我需要 std::forward,因为std::string::append支持std::stringconst char *std::string_view

I still have no idea why fold expressions need to be inside brackets, but seems this is how they need to do.我仍然不知道为什么折叠表达式需要在括号内,但似乎这就是他们需要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM