c++ 在切换范围v3的包含头的顺序后,编译失败

cwxwcias  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(74)

当我在range-v3 v0.12.0中同时使用views::set_intersectionviews::transform时,头文件range/v3/view/set_algorithm.hpprange/v3/view/transform.hpp的顺序很重要。如果先包含前一个头文件,代码将不会编译。相反的顺序编译效果很好。
这是一个最小的示例:(另请参见https://godbolt.org/z/nhK1s3xdv的实时演示)

#include <iostream>
#include <map>

// this order fails to compile, however, switch the order, it compiles
#include <range/v3/view/set_algorithm.hpp>
#include <range/v3/view/transform.hpp>

int main () {
    std::map<int, int> m1 {{0, 0}, {1, 1}, {2, 2}};
    std::map<int, int> m2 {{0, 0}, {2, 1}, {3, 2}};
    {
        std::cout << "set_intersection\n";
        auto res = ranges::views::set_intersection(m1, m2);
        for(auto&& p : res) {
            std::cout << p.first << ' ' << p.second << '\n';
        }
    }

    {
        std::cout << "set_intersection\n";
        auto res = m1 | ranges::views::set_intersection(m2);
        for(auto&& p : res) {
            std::cout << p.first << ' ' << p.second << '\n';
        }
    }
    {
        std::cout << "set_intersection and pipe\n";
        auto res = ranges::views::set_intersection(m1, m2)
                     | ranges::views::transform(
                        [](auto&& p) { return 0; });
    }

    return 0;
}

字符串
有趣的是,错误消息为

error: use of undeclared identifier 'bind_back'
                return make_view_closure(bind_back(set_intersection_base_fn{},


此行的

auto res = m1 | ranges::views::set_intersection(m2);


这和views::transform没有关系,
我搜索了bind_back,它定义在meta/meta.hpp中,并且已经包含在view/set_algorithm.hpp中。
我不明白窃听器在哪。

vyswwuz2

vyswwuz21#

使用模板时,包含顺序很重要。range-v3是大量模板化的,最好不要偏离要包含哪些头文件的文档。您找到的bind_back嵌套在命名空间meta中。您需要的bind_back在bind_back.hpp中定义,请参阅range-v3文档。

相关问题