尝试将std::accumulate误用为算法(为什么无论如何它仍在“数字” header 中?;))

template<class Range, class Seperator>
std::string strjoin(Range&& range, Seperator&& seperator) {
    if (std::empty(range)) {
        return "";
    }
    return std::accumulate(std::begin(range) + 1, std::end(range), std::ostringstream{} << *std::begin(range), [seperator](auto&& lhs, auto&& rhs) { return lhs << seperator << rhs; }).str();
}

Compiler explorer

上面的代码没有被编译,并且有点“天真”,但是我认为它突出了“问题”。

来自编译器资源管理器链接的错误:
<source>(13): error C2280: 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream(const std::basic_ostream<char,std::char_traits<char>> &)': attempting to reference a deleted function

C:/data/msvc/14.22.27905/include\ostream(57): note: see declaration of 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream'

C:/data/msvc/14.22.27905/include\ostream(57): note: 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream(const std::basic_ostream<char,std::char_traits<char>> &)': function was explicitly deleted

C:/data/msvc/14.22.27905/include\numeric(72): note: see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> strjoin::<lambda_d85bd1f8d5f8c34784482fe135d50702>::operator ()<std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>,int&>(std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &&,int &) const' being compiled

<source>(13): note: see reference to function template instantiation '_Ty std::reduce<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,_Ostr,strjoin::<lambda_d85bd1f8d5f8c34784482fe135d50702>>(const _InIt,const _InIt,_Ty,_BinOp)' being compiled

        with

        [

            _Ty=std::ostringstream,

            _Ostr=std::ostringstream,

            _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>,

            _BinOp=strjoin::<lambda_d85bd1f8d5f8c34784482fe135d50702>

        ]

<source>(19): note: see reference to function template instantiation 'std::string strjoin<std::vector<int,std::allocator<int>>&,const char(&)[4]>(Range,Seperator)' being compiled

        with

        [

            Range=std::vector<int,std::allocator<int>> &,

            Seperator=const char (&)[4]

        ]

C:/data/msvc/14.22.27905/include\numeric(72): error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::basic_ostream<char,std::char_traits<char>>' (or there is no acceptable conversion)

C:/data/msvc/14.22.27905/include\sstream(488): note: could be 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>::operator =(const std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &)'

C:/data/msvc/14.22.27905/include\sstream(468): note: or       'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>::operator =(std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>> &&)'

C:/data/msvc/14.22.27905/include\numeric(72): note: while trying to match the argument list '(_Ty, std::basic_ostream<char,std::char_traits<char>>)'

        with

        [

            _Ty=std::ostringstream

        ]

Compiler returned: 2

一个代码片段,供那些对如何使用普通for循环编写算法感兴趣的人使用:
template<class Range, class Seperator>
std::string strjoin(Range&& range, Seperator&& seperator) {
    if (!std::empty(range)) {
        std::ostringstream oss{};
        oss << *std::begin(range);
        for (auto&& e : iter_range(std::begin(range) + 1, std::end(range))) {
            oss << seperator << e;
        }
        return oss.str();
    }
    return "";
}

最佳答案

std::accumulate 要求“累加器”(第三个参数)为CopyAssignableCopyConstructiblestd::ostringstream都不是,因此您无法使用它。

您将需要使用其他方法,例如std::for_each,使用accumulate,但是要使累加器成为您附加到的std::string或其他方法。

10-06 05:07
查看更多