C++20 Ranges in Practice - Tristan Brindle - CppCon 2020

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ส.ค. 2024

ความคิดเห็น • 43

  • @bernadettetreual
    @bernadettetreual 3 ปีที่แล้ว +25

    Instead of piling even more onto , splitting this mega header would be nice.

    • @SimonToth83
      @SimonToth83 2 ปีที่แล้ว +7

      We will all be doing import std; in C++23, so what is in which header will not really matter.

  • @burakkirazli3759
    @burakkirazli3759 3 ปีที่แล้ว +14

    what a high quality voice presentation!

  • @MateiTene
    @MateiTene 3 ปีที่แล้ว +26

    For anyone looking for Tristan's introductory talk on ranges, which precedes this one, here is the link: th-cam.com/video/SYLgG7Q5Zws/w-d-xo.html

  • @andrewnaplavkov5485
    @andrewnaplavkov5485 3 ปีที่แล้ว +14

    You can improve trim_back on 58:36
    inline constexpr auto trim_back = views::reverse | trim_front | views::reverse;

    • @Omnifarious0
      @Omnifarious0 3 ปีที่แล้ว +2

      Of course, this way of implementing trim_back means that it only works on reversible ranges, and not on forward ranges.

    • @tcbrindle
      @tcbrindle 3 ปีที่แล้ว +6

      Indeed, well spotted! :)

  •  3 ปีที่แล้ว +7

    Why doesn't compilation just fail right away when passing an r-value reference to a range...? Instead it produces a noop object that produces a very confusing message when you try to use it . I wouldn't call that "really cool" :(

    • @toyamihiyami7941
      @toyamihiyami7941 3 ปีที่แล้ว

      But is that dangling iterator really a problem? You wouldn't want to dereference it without checking against container.end() anyways and at that point you would notice your mishap. For example if the container given to min_element is empty, then even if it wasn't dangling you would have a problem trying to dereference it.

    • @tcbrindle
      @tcbrindle 3 ปีที่แล้ว +7

      I answered a question about this at the end... basically it would stop you doing useful things, like ranges::copy(get_vector(), output_iter); where you generally don't care that the returned iterator is dangling, because you don't use it. I actually think that the error message is helpful once you know what ranges::dangling is, certainly moreso than the "no matching overload" you'd get otherwise.

  • @70_deveshupadhyay53
    @70_deveshupadhyay53 2 ปีที่แล้ว +1

    This is one of the best channels I found for C++, and I'll be forever on my recommendation list for anyone who practices C++.
    Can anyone suggest a similar channel for java?

    • @natedoggstyle
      @natedoggstyle ปีที่แล้ว +1

      4 months and no answers on this question. Java seems to lack the same vibrancy as C++ in its developer community.

    • @not_ever
      @not_ever ปีที่แล้ว +1

      @@natedoggstyle Or maybe Java developers aren't watching cppcon videos? I'm not watching Java videos and wouldn't, even if you paid me.

  • @pruibiebehastoet1914
    @pruibiebehastoet1914 2 ปีที่แล้ว +3

    The example at 12:40 does not compile because string_view{}..}.begin() returns an iterator to const

    • @tcbrindle
      @tcbrindle 2 ปีที่แล้ว +1

      Oops! Well spotted

  • @MaitreBart
    @MaitreBart 2 ปีที่แล้ว +2

    About trim_back: I'm new to ranges/views. My understanding is you cascade views using | op. They are not executed at definition/declaration. They become active when used in an algorithm where iterator(s) browse a collection (here, a string of char). Am I right until here?
    If so, trim_back is composed of 3 operations. My question is: Will those 3 operations be executed once per string (so the iterator runs on the final resulting view) or once per iterator step (i.e for each single char in the string)?

  •  3 ปีที่แล้ว +2

    Great video!

    • @CppCon
      @CppCon  3 ปีที่แล้ว

      Thanks for the visit

  • @MrDarcamo
    @MrDarcamo 3 ปีที่แล้ว +3

    Great presentation. In the end, when using inline constexpr variables for trim_front, trim_back, and trim_str, why not defining trim_str as an inline constexpr as well? Just because it could be confusing to whoever uses trim_str?

    • @tcbrindle
      @tcbrindle 3 ปีที่แล้ว +5

      Thanks :) I just kept trim_str as a function to show how to use "ranges-style" pipelines in ordinary functions which take strings and vectors etc. You could replace it with an inline constexpr function object if you wanted to.

  • @nelsonlee3376
    @nelsonlee3376 2 ปีที่แล้ว +1

    Can trim_str be constexpr?

  • @kuhluhOG
    @kuhluhOG 2 ปีที่แล้ว +1

    23:25 I can already feel that a lot of programmers are just going to ignore the "constant time" constraint and enable that vector can be treated like a view.

  • @multiHappyHacker
    @multiHappyHacker 2 ปีที่แล้ว

    I wonder if there is a concept for valid_iter_type or something? To screen out template instantiation with a ranges::dangling ?

  • @ruadeil_zabelin
    @ruadeil_zabelin 3 ปีที่แล้ว +2

    What is the overhead of views::common? Is there a known overhead or does it end up getting optimized out?

    • @tcbrindle
      @tcbrindle 3 ปีที่แล้ว

      The iterator type is something very like std::variant, where I and S are the original iter and sentinel types. I've never measured the overhead, but I'd be curious to know what it is (if anything).

  • @eugnsp
    @eugnsp 2 ปีที่แล้ว +3

    I was expecting to see `accumulate` at the end of a pipeline.

  • @mapron1
    @mapron1 3 ปีที่แล้ว

    38:58 - don't you need to pass std::plus because "defaulted" machinery won't work for comparison function as expected?

    • @tcbrindle
      @tcbrindle 3 ปีที่แล้ว

      No, it can be defaulted as written, see gcc.godbolt .org/z/zbGE8a for example

  • @alextrotta796
    @alextrotta796 3 ปีที่แล้ว +1

    At 57:29, I'm fairly sure the inline isn't necessary, as constexpr implies inline.

    • @eugnsp
      @eugnsp 2 ปีที่แล้ว +5

      It is necessary. constexpr doesn't imply inline for variables.

    • @eldarzakirov5571
      @eldarzakirov5571 2 ปีที่แล้ว +1

      For preciseness, constexpr doesn't imply inline for namespace scope variables, but for static member variable it implies inline.

  • @elliott8175
    @elliott8175 3 ปีที่แล้ว

    Link to previous talk: th-cam.com/video/SYLgG7Q5Zws/w-d-xo.html

  • @PaulTopping1
    @PaulTopping1 2 ปีที่แล้ว +1

    I'm not faulting the presenter but I can't help conclude that C++ is growing very complicated. Views and borrowed ranges, for example, are similar but different and one can be converted into another. Isn't that the very definition of confusion? I realize that each of these language features solves a problem but the added mass to the language may well sink it. We hear it said that there's a powerful new language embodied in C++20 but is it really that clean? How would it compare to a language built from scratch around the same goals? In other words, how close is C++20's baggage to zero? It would be great for someone to create a C++ variant that embodies all the good ideas in C++20 without all the deprecated stuff and (important) all the compromises forced on the language by backward compatibility.

    • @VFPn96kQT
      @VFPn96kQT 2 ปีที่แล้ว

      At that point you can use a totally different language. Why call it C++ if it doesn't compile vast majority of C++?
      Use #Rust or #Dlang

    • @christianchung9412
      @christianchung9412 ปีที่แล้ว +1

      have you heard of cppfront/cpp2 from Herb Sutter? it's a project exactly as you described

  • @farway-417
    @farway-417 3 ปีที่แล้ว

    The CppCon 2019 talk: th-cam.com/video/SYLgG7Q5Zws/w-d-xo.html

  • @MaceUA
    @MaceUA 3 ปีที่แล้ว

    What's that all about with `ranges::dangling`? Why?!
    It must be totally possible to return an "iterator" (a thing that behaves like an iterator) which also contains a moved copy of the input range in its internal state -- this way it would all work properly for rvalues. Why such a weird decision from the committee instead?

    • @hacker2ish
      @hacker2ish 3 ปีที่แล้ว

      But having such an iterator that points to 1 isolated element is weird, what happens when you increment it? It is effectively as if you've created another range with length 1 and returned it. And of course that element has to live somewhere and the iterator is just pointing to it, then the lifetime of that element would have to be extended and it would require a dynamic allocation.

    • @xarcaz
      @xarcaz ปีที่แล้ว

      @@hacker2ish Wouldn't it still just contain the range, just that it would increment which element within it that the iterating is pointing to?

  • @marcinbuchwald9585
    @marcinbuchwald9585 3 ปีที่แล้ว

    24:47 std::accumulate is more then this.

    • @tcbrindle
      @tcbrindle 3 ปีที่แล้ว

      In what way?