The Ranges Library in C++20: More Design Choices

The ranges library in C++20 made due to performance reasons a few unique design choices. These choices have consequences: cache issues and constness issues.

Here is a short reminder. In my last post, “The Ranges Libray in C++20: Design Choices“, I presented this possible implementation of std::ranges::filter_view:

if constexpr (!ranges::forward_range<V>)
    return /* iterator */{*this, ranges::find_if(base_, std::ref(*pred_))};
else
{
    if (!begin_.has_value())
        begin_ = ranges::find_if(base_, std::ref(*pred_)); // caching
    return /* iterator */{*this, begin_.value())};
}

The key observation is that the begin iterator is cached for subsequent calls. This caching has two interesting consequences:

  • Don’t use a view on modified ranges.
  • Don’t copy a view.

Or, to put it positively: Use views directly after you have defined them.

There are more design choices you must know.

Constness

The member function of a view may cache the position. This implicates the following:

  • A function taking an arbitrary view should take its view by universal reference.
  • Reading two views concurrently may be a data race.

Let’s discuss the first consequence.

Take Arbitrary Views by Universal Reference

The function printElements takes its view by universal reference.

void printElements(std::ranges::input_range auto&& rang) {
    for (int i: rang) {
        std::cout << i << " ";  
    }
    std::cout << '\n';
}

printElements takes its argument by universal reference. Taking it by lvalue reference or by value is, in general, no option.

Rainer D 6 P2 500x500

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (open)
  • "Generic Programming (Templates) with C++": October 2024
  • "Embedded Programming with Modern C++": October 2024
  • "Clean Code: Best Practices for Modern C++": March 2025
  • Do you want to stay informed: Subscribe.

     

    Taking the argument by const lvalue reference may not work because the implicit begin call on the view could modify it. On the contrary, a non-const lvalue reference cannot handle rvalues.

    Taking the argument by value may invalidate the cache.

    Concurrent Reading of Views may be a Data Race

    The following program exemplifies the concurrency issue with views:

    // dataRaceRanges.cpp
    
    #include <numeric>
    #include <iostream>
    #include <ranges>
    #include <thread>
    #include <vector>
     
    int main() {
    
        std::vector<int> vec(1'000);
        std::iota(vec.begin(), vec.end(), 0);
    
        auto first5Vector = vec | std::views::filter([](auto v) { return v > 0; }) 
                                | std::views::take(5);
    
        std::jthread thr1([&first5Vector]{
            for (int i: first5Vector) {
                std::cout << i << " ";  
            }
        });
    
    
        for (int i: first5Vector) {
            std::cout << i << " ";  
        }
    
        std::cout << "\n\n";
        
    }
    

    I iterate in the program dataRaceRanges.cpp concurrently two times through a view in a non-modifying way. First, I iterate in the std::jthread thr1 and second in the main function. This is a data race because both iterations implicitly use the member function begin, which may cache the position. ThreadSanitizer visualizes this data race and complains about a previous write on line 24: std::cout << i << " ";

    On the contrary, iterating through a classical container such as std::vector is thread-safe. There is an additional difference between classical container and views.

    Propagation of Constness

    Classical container model deep constness. They propagate their constness to their elements. This means that modifying elements of a constant container is impossible.

    // constPropagationContainer.cpp
    
    #include <iostream>
    #include <vector>
    
    template <typename T>
    void modifyConstRange(const T& cont) {
        cont[0] = 5;
    }
     
    int main() {
    
        std::vector myVec{1, 2, 3, 4, 5};
        modifyConstRange(myVec);         // ERROR
    
    } 
    

    The call modifyConstRange(myVec) causes a compile-time error.

    On the contrary, views model shallow constness. They do not propagate the constness to their elements. The elements can still be modified.

    // constPropagationViews.cpp
    
    #include <iostream>
    #include <ranges>
    #include <vector>
    
    template <typename T>
    void modifyConstRange(const T& cont) {
        cont[0] = 5;
    }
     
    int main() {
    
        std::vector myVec{1, 2, 3, 4, 5};
    
        modifyConstRange(std::views::all(myVec));   // OK
    
    }   
    

    The call modifyConstRange(std::views::all(myVec)) is fine.

    What’s Next?

    Coroutines are probably the most challenging part of C++20. My next post presents a guest post from Dian-Lun Lin. He will give a concise introduction to coroutines and exemplify his idea with a straightforward scheduler managing tasks.

    Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Stephen Kelley, Kyle Dean, Tusar Palauri, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery, Matt Godbolt, and Honey Sukesan.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Modernes C++ GmbH

    Modernes C++ Mentoring (English)

    Do you want to stay informed about my mentoring programs? Subscribe Here

    Rainer Grimm
    Yalovastraße 20
    72108 Rottenburg

    Mobil: +49 176 5506 5086
    Mail: schulung@ModernesCpp.de
    Mentoring: www.ModernesCpp.org

    Modernes C++ Mentoring,