range-v3 how to action :: join with delimiter

时间:2018-01-22 14:20:30

标签: c++ range-v3 visual-c++-2015

我从git获得MSVC的range-v3。并由MSVC C ++ 14编译器编译。

考虑代码:

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;

它按预期工作。 但我想添加分隔符加入,如果我写

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

代码未编译。怎么了? 编译器错误的质量如下:

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'

更新

不仅MSVC端口行为如此,而且here也是

1 个答案:

答案 0 :(得分:2)

猜测:字符串文字不是一个范围。请尝试将std::string(",")view::c_str(",")传递给view::join

更新:啊。试试这个(用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;
}