Functors() - Function objects - functions with state | Modern Cpp Series Ep. 99

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

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

  • @kenk4552
    @kenk4552 6 หลายเดือนก่อน +5

    Your modern Cpp series is a hidden gem. Will for sure get more views!!

    • @MikeShah
      @MikeShah  6 หลายเดือนก่อน

      Cheers!

  • @buddyairguy2249
    @buddyairguy2249 10 หลายเดือนก่อน +2

    As usual -> Great lesson!

    • @MikeShah
      @MikeShah  10 หลายเดือนก่อน

      Cheers!

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

    "A functor is an instance of type T whose call operator () is overloaded". This implies state.

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

    I guess this is not the same as functors from category theory, where functors are a mapping from one category to another while preserving structure. So for example transforming one container into another. This feels more like a monad in which you wrap some object in another.

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

      Indeed, not as strict as the category theory definition what I'm showing here, but a monad would be more related. :)

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

    Hi Mike, thank you for another great video! I do have one question: when you mention "we can take advantage of move assignment" at 10:15 by using the initializer list for the constructor, what do you mean exactly?

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

      I think in the sense that we construct the attribute mHealth (by assigning it the value h) and the object Goblin at the same time. Instead of a possible copy would occur if an object is already present in the memory
      Let's see what coach Mike will respond 😀

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

      I have the same question

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

      ​@@Southpaw101 Hi team, that's correct. We avoid a copy with initializer list (th-cam.com/video/vjAJ4ESX1Jc/w-d-xo.html) and then use something like: m_health(std::move(health)) and avoid making extra copies.

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

    give the goblin his gold

  • @LeflxLight
    @LeflxLight 8 หลายเดือนก่อน +1

    Thank you very much for the video , Its really amazing !!! These videos really help me clear my concepts ! Everything is clearly or smoothly explained with perfect pace. Loved it😀😀

    • @MikeShah
      @MikeShah  8 หลายเดือนก่อน

      Cheers!

  • @kirandhegaskar9298
    @kirandhegaskar9298 10 หลายเดือนก่อน +1

    Hi mike in sort function third parameter we are calling goblincomparator and then our functor gets called ,but is this right way to called functor ,because whenever we write goblincomparator() it should called constructor not functor

  • @michapawlik6501
    @michapawlik6501 9 หลายเดือนก่อน +1

    You literally solved the problem I had with writing a better version of std::vector that would filter data. 3 lines of code and couple hours of work spared

    • @MikeShah
      @MikeShah  9 หลายเดือนก่อน

      Cheers, happy to hear that!

  • @peufipeufhhh2405
    @peufipeufhhh2405 2 หลายเดือนก่อน

    Where can we get those slides?

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

    Thanks for the vid! Is the comparator callable object more like a general function since it’s not storing any state? You could swap it out with a lambda and have the same effect right?

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

      For conparator, could use general function if no state needed, can also use things like std::less or lambda.

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

    I want to use one function from other cpp file in other cpp file without include. I mean I want to use functor here. The functor will be a callable function and I am going to use it in curl. Do you have any idea?

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

      Hmm, you need at the least a forward declaration for that function (e.g. something like myclass::operator()).

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

    Than you for the excellent teaching. If I may, I'd like to know how this works internally.

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

      Thank you for the kind words! The () operator being overloaded is the key here for how this is working.

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

    Yes very clear examples but one doubt - why are you returning NewResult which is passed in - instead of m_result1 and m_result2?

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

      I suppose they're the same value -- probably better to return the member as you suggest

  • @nicholashanson9508
    @nicholashanson9508 2 หลายเดือนก่อน

    i didn't get the advantage of a functor over using the member function for comparison

    • @MikeShah
      @MikeShah  2 หลายเดือนก่อน

      The functor allows us to pass 'different' 'function objects' while the program is running -- i.e. we can change how objects are compared at run-time, versus just having the member function which gives us exactly 1 comparison. We could do some things like pass a function pointer or something into our comparison operator, but it's more extensible (and provides a single responsibility) to just have a separate 'functor' for the comparison.

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

    As always thanks Mike. Do you have any videos on std::function?

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

      Cheers! I have one planned for sometime in the future 🙂

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

      @@MikeShah Awesome! maybe in regards to a theoretical callback scenario? :)

  • @xbz24
    @xbz24 10 หลายเดือนก่อน +1

    Cheers

    • @MikeShah
      @MikeShah  9 หลายเดือนก่อน

      🥂

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

    this just seems like a needlessly complicated way of creating an object

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

      Lambda's behind the scenes are just functors. They're a necessary building block for carrying state for a function call. Can actually be quite useful. As always, right tool for right job should be used.

  • @HousseinAlDroubi
    @HousseinAlDroubi 4 หลายเดือนก่อน

    Hey, good man, what about you.