Technology

CppCon 2019:From STL to Ranges

Jeff Garland

Intro

  • The beginning of the end – for begin and end

Basics

std::sort(a.begin(), a.end())

Instead:

ranges::sort(a);

Example with find_if

the ranges way: filter_view

for (int i : rng::filter_view (a, is_six)) { std::cout << i << “ “; }

Tristan Brindel NanoRange

Godbolt

  • supports several
  • range: something that can be iterator over
  • range algo: algorithm that takes range
  • view: lazy range thats cheap top copy
  • ranger adapter: turn range into a view

mechanics

  • 3 new names ranges, ranges::views, views = ranges::views

Why new namespaces

  • behavior and guaranteed of some algorithms are changed

What’s a range

  • iterator pair is simplest
  • sentinel and iterator can be different types
  • ranges can be infinite
  • any container with begin()/end() can be used in a range

Views are ranged with lazy evaluation

  • non-owning of elements
  • all methods
    O(1)

    for copy and assignment

range adaptors -> views from ranges

  • pipeline syntax

filter_view adaptor

for (int i = range_filter_view (vi, is_event))

No more for loops

Range Algorithm details

  • list of range algorithms

Projection parameters

  • provides first class filtering predicate
  • comes from the Adobe Source Libraries (ASL)

ranges::sort(vistuff, std::less<>{}, [](auto const &i) {return i.value;})

Views and View Adaptor details

How do ranges perform

  • Compile time: no noticeable difference
  • runtime: nothing expected over stl algorithms

Resources

wg21.link/n4810

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.