Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

First example doesn't look bad in C++23:

    auto get_ids(std::span<const Widget> data)
    {
        return data
            | filter(&Widget::alive)
            | transform(&Widget::id)
            | to<std::vector>();
    }


To me, the cool (and uncommon in other languages' standard libraries) part about C++ ranges is that they reify pipelines so that you can cut and paste them into variables, like so:

    auto get_ids(std::span<const Widget> data)
    {
        auto pipeline = filter(&Widget::alive) | transform(&Widget::id);
        auto sink = to<std::vector>();
        return data | pipeline | sink;
    }


This looks awesome!

I'm really want to start playing with some C++23 in the future.


I cheated a bit, I omitted the namespaces. Here's a working version: https://godbolt.org/z/1rE9o3Y95


This is not functionally different from operator<< which std::cout has taught us is a neat trick but generally a bad idea.


Unlike the iostreams shift operators, the ranges pipe operator isn't stateful.


There's state when you try to use the final result, though. It's not threadsafe due to caching.

https://www.youtube.com/watch?v=c1gfbbE2zts




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: