range-v3 how to action::join with delimiter

半世苍凉 提交于 2019-12-08 12:41:39

问题


I get range-v3 for MSVC from git. And compile by MSVC C++14 compiler.

Consider code:

auto getter2 = [](const std::string&r) { return r+r; };
std::vector<std::string> vv = { "11","22", "33" };
std::cout << (vv | view::transform(getter2) | action::join) << std::endl;

It works as expected. But I want to add delimiter to join and if I write

std::cout << (vv | view::transform(getter2) | action::join(",")) << std::endl;

The code does not been compiled. What is wrong? Mass of compiler error is below:

1>d:\sources\ranges_test\ranges_test.cpp(28): error C2672: 'operator __surrogate_func': no matching overloaded function found
1>d:\sources\ranges_test\ranges_test.cpp(28): error C2783: 'unknown-type ranges::v3::action::action<ranges::v3::action::join_fn>::operator ()(Rng &,Rest &&...) const': could not deduce template argument for '__formal'
1>d:\sources\fingrad\dev.fingrad\src\vc\lib\range\v3\action\action.hpp(120): note: see declaration of 'ranges::v3::action::action<ranges::v3::action::join_fn>::operator ()'
1>d:\sources\ranges_test\ranges_test.cpp(28): error C2893: Failed to specialize function template 'unknown-type ranges::v3::action::action<ranges::v3::action::join_fn>::operator ()(T &&,Rest &&...) const'
1>d:\sources\ranges_test\ranges_test.cpp(28): note: With the following template arguments:
1>d:\sources\ranges_test\ranges_test.cpp(28): note: 'T=const char (&)[2]'
1>d:\sources\ranges_test\ranges_test.cpp(28): note: 'Rest={}'
1>d:\sources\ranges_test\ranges_test.cpp(28): note: 'A=ranges::v3::action::join_fn'

UPDATE

Not only MSVC port behaviers so but here too


回答1:


Guessing: A string literal isn't a range. Try passing std::string(",") or view::c_str(",") to view::join instead.

UPDATE: Ah. Try this instead (tested with clang trunk):

#include <range/v3/all.hpp>
#include <vector>
#include <iostream>
int main()
{
    using namespace ranges;
    std::vector<std::string> vv = { "11","22", "33" };
    auto getter2 = [](auto&r) { return view::concat(r, r); };

    std::cout << (vv | view::transform(getter2) 
                     | view::join(',')
                     | to_<std::string>()) << std::endl;
    return 0;
}


来源:https://stackoverflow.com/questions/48383671/range-v3-how-to-actionjoin-with-delimiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!