Should I pass by const reference or by value?

Sdílet
Vložit
  • čas přidán 7. 03. 2023
  • Support ► / thecherno
    Instagram ► / thecherno
    Twitter ► / thecherno
    Discord ► / discord
    Code ► github.com/MariuszDot/Game_Fl...
    Related C++ videos
    lvalues and rvalues in C++ ► • lvalues and rvalues in...
    Move Semantics in C++ ► • Move Semantics in C++
    Links to stuff I use (helps support the channel):
    ⌨ Keyboard ► geni.us/T2J7
    🐭 Mouse ► geni.us/BuY7
    💻 Monitors ► geni.us/wZFSwSK

Komentáře • 366

  • @youssefmohamedabbashabib613

    Absolutely good idea. Divide the videos into multiple topics 🙏

  • @RekiDragunova
    @RekiDragunova Před rokem +263

    As a newbie programmer in C/C++, I use const references all the time, but I never truly understand when or why I should pass by const reference or by value. I learned a lot from today's video and am looking forward to seeing short films that focus on specific topics. Huge thanks!

    • @colly6022
      @colly6022 Před rokem +4

      personally, i like to assume a default for how i use language features, and then get more specific (e.g., using "const", references, pointers, typename vs classname, etc..) when that clarity becomes important.
      assume the default "int foo(int x)", and then attach all the modifiers that declare more specificity of your function.
      ex:
      float sin(float th) -> float sin(const float th) -> constexpr float sin(const float th)

    • @DrGreenGiant
      @DrGreenGiant Před rokem +14

      The main takeaway is, if it fits in a register then the compiler can keep the VALUE in the register (and optimisation to avoid stack read writes.) No memory bus access required.
      A reference *is* a pointer in hardware terms, which always requires memory access because it's hard to optimise, which is comparatively slow.
      Source: I'm a bare metal C++ firmware engineer and this stuff makes a huge difference in my 32 bit world!

    • @mapron1
      @mapron1 Před rokem +2

      I am trying to pass by value whenever it's possible to avoid extra copies. Because, you know, move exists. And it is more efficient to pass std::vector by value (also don't have problems with aliasing if you pass TWO vectors). And almost every other 'expensive copy' as well.

    • @ajtan2607
      @ajtan2607 Před rokem +5

      @mapron1
      "I am trying to pass by value whenever it's possible to avoid extra copies."
      That sounds quite contradictory, don't you think? Passing by value means copy construction which logically implies that a copy is to be created.
      "Because, you know, move exists."
      I don't think move semantics should be involved as it serves a different purpose. Moving logically implies transferring of content and resources, which is different from just trying to read data.
      `std::vector` becomes more and more expensive to copy the more elements it has.
      In most cases, the point of accessing the value through a const reference is to avoid unnecessary copying of read-only data. For trivial types that can fit in a CPU register, sure, a copy is fine. But for types like `std::vector`, you might want to use references instead.

    • @mapron1
      @mapron1 Před rokem

      @@ajtan2607 Passing by value means copy construction which logically implies that a copy is to be created.
      That just not true. modern C++ is all about value semantics.
      > But for types like `std::vector`, you might want to use references instead.
      No you don't. Just get used to move everywhere.
      if you don't need vector data, only like, well, reference - don't pass a vector, pass std::span argument. otherwisse 99% you need pass by value.
      sigh... Understandable, beating 30 years of habits is hard.
      Also you need to be consistent - taking my advice in legacy codebase when only file follows new rules is just a harm.

  • @robertmoats1890
    @robertmoats1890 Před rokem +38

    A completely agree with you that passing primitive data types by reference is not a great idea. It will most likely prevent compiler optimizations. However, I think its probably a good idea to pass custom types by reference, even if they are small. The reason being that their size (and internal behavior) can easily change in the future, without the programmer later realizing that these function parameters all need updated to reference types. After 20 years of programming, one thing I've learned is to treat everything I've touched as unstable and dynamic.

  • @Lockdheart
    @Lockdheart Před rokem +103

    I heartily approve of doing the code reviews by topic. If it tells you anything, this is the very first one of your code reviews I've ever watched--specifically because it wasn't ridiculously long, and I could see in the title that it was about something I might find useful.
    You might get fewer views per individual video this way, but I have a feeling you'll get *way* more total views overall.

    • @m4l490n
      @m4l490n Před rokem +5

      I don't agree, I think he'll get even more views per video because they'll be short and more focused.

    • @emmanuelvazquez5036
      @emmanuelvazquez5036 Před rokem +1

      Same here. I once started like a 1h video of code review, but quickly I got lost and never finished.
      Instead, for this quick video I already entered knowing what was the topic, was direct to the point, easier to understand and, well, I saw to the end.

  • @seargd1
    @seargd1 Před rokem +26

    This was definitely more easily digestible and focussed than 40 minutes of code review that goes in five different directions. I smashed like so hard on this that I almost dropped my phone.

  • @mateuszabramek7015
    @mateuszabramek7015 Před rokem +9

    That "it gots darker here" got me 😂

  • @vitormoreno1244
    @vitormoreno1244 Před rokem +24

    Divide by multiple topics, that will help your channel on the long term as more videos will popup on CZcams search. And that will help less skilled programmers(like me) to learn specific topics.

  • @aronseptianto8142
    @aronseptianto8142 Před rokem +15

    this is absolutely a better format, not only because my attention span is garbage
    but also it really helps to build a library of "advice" video that's easily searchable even without the context of the code review
    this kind of advice is quite rare in a normal beginner tutorial and StackOverflow can be quite asinine about it
    this way your video can reach a broader audience who just need specific advice

  • @stvreumi
    @stvreumi Před rokem +3

    I like the idea of splitting the code review video by topic! It taught me a lot. Hope to see you sharing more videos like this in the future.

  • @fabiannedelcu3492
    @fabiannedelcu3492 Před rokem +5

    I really like the short, by topic code review!

  • @Balouxe
    @Balouxe Před rokem +15

    This format is a good idea, and you could also add this video to the C++ series as it covers a pretty interesting topic that fits into it ! Cheers

    • @battosaijenkins946
      @battosaijenkins946 Před rokem

      Heheheh... i know fools who const reference everything even boolean types and I'm like... 🤦‍♂🤦‍♂🤦‍♂

  • @dXXPacmanXXb
    @dXXPacmanXXb Před rokem +3

    9:43 yes! this is a fantastic idea. I only clicked on this because the title was interesting. I never click on code review videos

  • @amayesingnathan
    @amayesingnathan Před rokem +2

    100% love the shorter videos. Recently I haven't been able to find the time watch longer code review videos, so breaking them down makes it much easier

  • @harleensingh2531
    @harleensingh2531 Před rokem

    As you said, a big video on this would be really cool. Thanks for all your work :)

  • @RelayComputer
    @RelayComputer Před rokem +19

    Just look at the assembly code my friend, I bet you will get a couple of surprises, particularly with private methods. Hint: the compiler does not always honour the argument passing convention you specified

    • @andyyy1094
      @andyyy1094 Před rokem

      can you go into a bit more detail? what exactly are you referring to

    • @RelayComputer
      @RelayComputer Před rokem +6

      ​@@andyyy1094 I mean that you can write code meant to pass or return parameters by value, particularly long structs, but the compiler may decide to pass them by constant reference if such parameters are not modified inside the function and the function is contained in the same translation unit (C static function, or C++ private method). Furthermore, even if the function is not private, some compilers can go a step further and create two versions of the same function, one to be called (often inlined) using references when invoked from the same translation unit (regardless of what passing convention you specified), and another one ready for external calls. However, what is said in the video is correct and programmers should keep with good practices and never assume that the compiler will optimise everything

    • @urisinger3412
      @urisinger3412 Před 11 měsíci

      Dont trust the compiler on everything, especially if you want to run things in debug mode.

    • @RelayComputer
      @RelayComputer Před 11 měsíci

      @@urisinger3412 What you mean by don't trust the compiler? Compilers are by far the most trustable pieces of software in existence. Can't imagine the mess if it wasn't like that

    • @urisinger3412
      @urisinger3412 Před 11 měsíci

      @@RelayComputer dont trust the compiler to optimize every stupid thing you do, they dont catch everything and those little things can add up

  • @PuraTvOyun
    @PuraTvOyun Před rokem +1

    Great Idea go for it. That is the exact reason why I watch long code review videos.
    At any random time in a video, you give a specific knowledge for a specific situation that normal learning seriea doesn't even think about to teach us.
    For example this video enlighten me about r and l-values. Thank you for that. Learned C++ with your C++ series years ago and now I can develop custom tools for the games I make with Unreal Engine.

  • @fyntyaka
    @fyntyaka Před rokem +1

    Making small videos for certain topics is very good idea. Never watch your code review videos before, but watched this video because of intresting topic. Often was asking myself the same question, and now I have an answer. Thanks a lot!

  • @m4l490n
    @m4l490n Před rokem +5

    I just want to join all those that have said that it is an excellent idea to divide the code reviews into smaller videos focusing on one concept. It is super useful!!! Also, you get the opportunity to explain the same concept in multiple and different code sets which will greatly help understand better the concept, you know, by looking at it from different angles and use cases.

  • @kent8547
    @kent8547 Před rokem

    I look forward to your bigger video on this :) I also enjoy this style of code review more, it's more descriptive to what the video goes over

  • @brunooliveirasoares7489

    Such a good idea!
    Please, more of these short videos!

  • @animarain
    @animarain Před rokem

    Yes!!! Please do that! Splitting it into parts and explaining different topic on each video would be so much helpful!

  • @arispacegirl
    @arispacegirl Před rokem

    making short code review responses that address very specific topics would be perfect for...well, shorts!
    I've seen a couple channels try out short "tips & tricks" videos around code and they've been very useful!!

  • @FatalExceptionz
    @FatalExceptionz Před rokem +29

    Another thing to consider is that unnecessary passing by pointer/reference can also prevent the compiler from making certain microoptimizations

    • @DaddyFrosty
      @DaddyFrosty Před rokem +2

      Oh shit I thought Const ref would help as opposed to impede that

    • @teranyan
      @teranyan Před rokem +5

      @@DaddyFrosty It is exactly like that. Using const ref gives the compiler perfect information, it can choose to copy or not the uint. People who don't understand this and just spit out the "pass small types by value" ideology from 30 years ago, are frankly clueless. Const reference isn't some promise to use a pointer, it represents the abstraction of a read only object that already exists somewhere.

    • @piechulla1966
      @piechulla1966 Před rokem

      @@teranyan C++ Core Guidelines F.16: For “in” parameters, pass cheaply-copied types by value and others by reference to const

    • @ABaumstumpf
      @ABaumstumpf Před rokem +1

      @@teranyan It depends on the function.
      Say something like "void multiplyVectorByValue( std::vector& vec, const& val)" - iterating over the vector and multiplying every member by val. here passing by reference can be a serious detriment if the compiler can not proof that "val" is not part of "vec" - cause then it has to re-load val on every iteration, preventing any loop-hoisting or vectorisation.

  • @anon_y_mousse
    @anon_y_mousse Před rokem +5

    This is one of the few times when I actually agree with the design of C++. Since references are mostly transparent you can start with just passing by value and should you change to a more complex object in the future merely change the signature to include a const reference and have little trouble. Some languages make the boneheaded decision of either removing pointers and explicit reference documentation and everything is a reference. Some make the equally boneheaded decision of making references explicit all around which basically means they're just pointers. C++ actually did it right here.

    • @Spongman
      @Spongman Před rokem +2

      references are just non-nullable pointers with a '.' accessor instead of '->'. who knows why k&r chose '->' instead of '.', but hey now we're stuck with it.

  • @john.dough.
    @john.dough. Před rokem

    love seeing this short and sweet!!

  • @Spongman
    @Spongman Před rokem +3

    This video is good advice, it's basically F.16 in the cpp core guidelines: For “in” parameters, pass cheaply-copied types by value and others by reference to const. F.15 has a really good diagram that illustrates all the choices.
    IMO it's a good idea to keep your argument definitions 'const', even when receiving value types. Especially in larger methods it's useful to know for certain that a given argument hasn't changed.
    Also, don't use 'new', 'array'/'vector' would trivially give you the all-important move constructor for that buffer.

  • @DaveMacara89
    @DaveMacara89 Před rokem

    Very insightful as always Cherno thanks!

  • @decky1990
    @decky1990 Před rokem +3

    Really valid topic! Varies in a lot of things - threads and coroutines make things slightly trickier, but nice video

  • @YoungAdventures
    @YoungAdventures Před rokem

    I love this video format!

  • @cubevlmu385
    @cubevlmu385 Před rokem

    Fantastic topic, helps a lot about programming futures. Nice video

  • @twipps7700
    @twipps7700 Před 8 dny

    thank you for clearing this confusion on my part!!

  • @antonincarette8928
    @antonincarette8928 Před rokem

    Very good idea - the fact to show more shorted (but specific) videos is (imho) more interesting and more appealing than big review videos.

    • @antonincarette8928
      @antonincarette8928 Před rokem

      @@thecherno3 OMG thank you! What did I won?! I can’t believe it! It is amaaaaaaaaazing! I hope you’re not a fake OMG! I hope it is a house! Is it a house? Or a 35 levels building in the middle of Central Park?! Or, more amazing, 15kg of twinkies!!!! I hope this is not fake OMG! WHAT IS A DM? HOW TO DM? WHAT IS THE PURPOSE OF LIFE?! WILL « TRON 3 » BE A GOOD TRON SEQUEL? SO MUCH QUESTIONS!!!!!!!

  • @stavrosmalagaris3352
    @stavrosmalagaris3352 Před rokem +1

    Splitting a long code review into more specific and shorter videos is a fantastic idea. As you said anyone could focus on particular topics that may be intersted in and understand a topic better without being overwhelmed

  • @VadimHarenco
    @VadimHarenco Před rokem

    Love these series!

  • @djazz0
    @djazz0 Před rokem +2

    So happy I'm not coding in C++ anymore. I use Nim and it checks the size of what you pass and chooses by reference or value at compile time. So if you pass an object, it gets sent as reference if it's above a certain threshold. Ints gets passed by value unless you use "var" (mutable reference).

  • @Zarathustra0512
    @Zarathustra0512 Před rokem +4

    fwiw, the "built-in C++ type" is std::uint32_t. uint32_t is a C alias that most (all major) C++ std library implementations happen to provide, but isn't part of the std

  • @willchris7654
    @willchris7654 Před rokem +3

    I think, at least for me, it'll be easier to learn and digest your videos in a smaller chunks format like how you use to do C++ tutorial. It'll also be easier to see what specific topic you're covering specifically and if later I need to come back to it, it'll be easier to search as well. Just my thought though. You're still one of my best mentor for programming :)

  • @avtem
    @avtem Před rokem

    i really liked this one. It's short and it's about one specific topic.

  • @AntoniGawlikowski
    @AntoniGawlikowski Před rokem

    By topic sounds (and works in this instance) great!

  • @rhoward99
    @rhoward99 Před rokem

    Multiple topic videos per review would be great!

  • @justindavis5960
    @justindavis5960 Před rokem +1

    Definitely prefer this format more than a longer video as it is easily digestible. It could also be more useful for people who just need information on a specific topic

  • @TheJohndward01
    @TheJohndward01 Před rokem +1

    The part of the video where you said you should probably make an entire video about, I think it was around 2:15, that would be very helpful. The CPU is still very much unknown to me. And individual videos would be great especially as a playlist too.

  • @vincentpieterse1694
    @vincentpieterse1694 Před rokem

    Hi Cherno. Love your work! Could you please do a c++ video on implementing a finite state machine?

  • @matpietrzak
    @matpietrzak Před rokem +1

    Good idea. Shorter videos but more specific targeting a given concept or issue. It 's gonna be more interesting.

  • @xbz24
    @xbz24 Před 2 měsíci

    very deep video about basics , thx

  • @sanduchicu7545
    @sanduchicu7545 Před rokem +1

    Great video as always!
    The short form videos I think are a lot better because, as you said, the video is more on-topic but not all over the place.
    However, I think for every code review you should first make a video to overview the project and show the improvements that you would do, maybe explaining briefly (not focus on just one improvement, like the flying dog game video) and then make separate videos on every important topic, explaining it in detail (just like you did in this video).

  • @thelemonman2271
    @thelemonman2271 Před rokem

    Holy shit I searching for something like this just a day or two ago. Chreno-sama smiles upon me 🙏

  • @nuv3647
    @nuv3647 Před rokem

    Splitting it up into smaller more specific videos does seem like a good idea. Really liked this one and got a pretty in depth explanation. Unlike general code review videos where you probably wouldve just said a few things and then kept going.

  • @BlazingMarauderDCUO
    @BlazingMarauderDCUO Před rokem

    Explaining the "why" of anything is extremely valuable, at least imho. throughout all of your content that I have watched, the most enjoyable is when you explain quite trivial things but explain the reasoning at a really low level.

  • @sasha9245
    @sasha9245 Před rokem

    It's also nice to understand the difference between passing by value as 'const uint32_t' or 'uint32_t'. Assuming the argument is in a register, for 'const uint32_t' compiler is safe not to create a copy, while for 'uint32_t' it might allocate another register as this argument might be changed inside a function.
    For example:
    uint32_t sum(uint32_t n) { uint32_t sum = 0; while (n > 0) sum += n--; return sum; }

  • @0xKakashi
    @0xKakashi Před rokem +1

    Never thought of this whenever using passing by const reference. Cherno sir 👌

  • @vsevolodnedora7779
    @vsevolodnedora7779 Před rokem

    I found it useful, and I am more willing to watch a short video about a specific problem/thing in the code.

  • @KristofNachtergaele
    @KristofNachtergaele Před rokem

    This format would indeed be better than the massive code reviews, i skip those but this had a title that described what was being talked about and interested me and turned out to be useful.

  • @marcio603
    @marcio603 Před rokem

    I agree that dividing the videos will make them more "digestable" but your content is so good that I wouldn't mind to watch hours of video... I work with C++ nowadays and there some videos that I have watched several times... Thanks for the sharing your amazing knowledge.

  • @antonpieper
    @antonpieper Před rokem

    Good idea to split the review up👍

  • @ozguragcakaya9017
    @ozguragcakaya9017 Před rokem

    Go for it man!

  • @sourashismondal5060
    @sourashismondal5060 Před rokem +4

    Hi, can you please make a detailed video of how virtual table works in the context of virtual functions?

  • @ktoztam
    @ktoztam Před rokem

    As someone who was actually wondering about this thing in particular I very much aprove the idea of making code reviews as by topic rather than long format they used to be.

  • @LogicEu
    @LogicEu Před rokem

    Very real and useful advice going on here

  • @stefanhellwig-lp4rg
    @stefanhellwig-lp4rg Před rokem +1

    I also like the idea of splitting the code review into shorter videos. This let‘s one pick the topics of interest quickly and it‘s also easier to „digest“ ;-).

  • @redafakih12
    @redafakih12 Před rokem +1

    new idea is great!

  • @VictorOrdu
    @VictorOrdu Před rokem

    I agree. Good idea.

  • @artie5913
    @artie5913 Před rokem +4

    4:10 what if someone adds something big(like a bunch of strings) later in this CustomType? I don't think you want to fix every pass by value after this

  • @samuelmartin7319
    @samuelmartin7319 Před rokem

    I like the idea of splitting up the code review my topic.

  • @kiferdon7243
    @kiferdon7243 Před rokem

    I like your idea to divide videos

  • @TobotriebNightcore
    @TobotriebNightcore Před rokem +8

    I like the Qt style: pass c++ (std)types by value, Qt objects by reference, and Qt objects where you transfer ownership by ptr

  • @guilhermedabolsa927
    @guilhermedabolsa927 Před rokem

    Good idea Cherno! Clicked on this vid because it was not 01h and 40min long for example =)

  • @iandy_hd3743
    @iandy_hd3743 Před rokem +2

    finally a c++ video :)

  • @vmarcelo49
    @vmarcelo49 Před rokem

    I liked this format, short videos are better to be referred later too

  • @harleensingh2531
    @harleensingh2531 Před rokem

    I think dividing code review based on topics would be great!

  • @olo90
    @olo90 Před rokem +2

    Personally, both formats (longer video with timestamps) as well as short formats that are just focused on one part are fine. I'd say make the choice based on what gets more views (without having to resort to click-baity titles / screencaps with those "shocked face" looks).

  • @Sebanisu
    @Sebanisu Před rokem

    static_assert(std::is_trivially_copyable_t); Type trait can be used to test if your type is trivially copyable.

  • @markrudford3143
    @markrudford3143 Před rokem +1

    Well, you only need to use a const if that reference or data type is needed else ware in the code file. If not then go the value type route if it is not used anywhere else but once to declare a value.
    Dynamic vs static values used once or more. Depends..

  • @chases4951
    @chases4951 Před rokem +2

    Have you ever seen a case where passing by reference was better due to the reduced set of memory pages being used? I could imagine a case where it would help with TLB hits in the CPU?

  • @YeloFelo
    @YeloFelo Před rokem +2

    Love this style of broken-up code review, but it would be nice if there was some indication that this is actually a code review, like maybe on the thumbnail

  • @GreenClover0
    @GreenClover0 Před rokem

    Yeah, that sounds like a good idea ^^

  • @user-gl9yo8rz8k
    @user-gl9yo8rz8k Před rokem +1

    На современных процессорах объекты размером до 8 указателей (32/64 байта) можно смело передавать по значению. Без глубокого копирования указателей на кучу, естественно.

  • @aurinator
    @aurinator Před rokem +1

    Honestly surprised this isn't handled by the compiler.

  • @JohnWasinger
    @JohnWasinger Před rokem +1

    I listen to your videos as I drive. I like that the signal to noise from your talks is quite high. I’m not talking about static here. I feel like I’ve learned a lot from just listening to you.
    Some CppCon presentations are very useless to just listen to.

  • @malckhazarsteamcat9817
    @malckhazarsteamcat9817 Před rokem +6

    Oh, my dear Cherno.
    1. Roughly at 1:20 you said that const reference is converted to ptr. Actually, not just a "ptr *", but "ptr * const".
    2. Passing argument to a function depends on ABI. If your ABI declares that all arguments must be passed via stack - then yes, small types converted to pointer will take more memory.
    In old ABIs agruments was always passed via stack (see Intel x86 ABI as an example).
    In modern ABIs arguments mostly are passed via registers which always have same size. Only data with sizes higher than register size always passes via stack.
    The main difference here is number of accesses to a memory holding the value and it will be "long" if variable located in different memory page, which will have to be kept in RAM.
    3. At ~4:59 you saying that "type is not trivially copyable" is an issue of passing by value. This is not the sole reason. If your type is trivially copyable, but is a big sized (too many members/fields) - this is also an issue for passing by value, because you'll eat all the stack.

    • @user-cy1rm5vb7i
      @user-cy1rm5vb7i Před rokem +1

      clearly breaking abi is not an issue, if you're still in development, just recompile the binaries

    • @reductor_
      @reductor_ Před rokem

      With (2) if your passing by reference then the reference needs to point to a place in memory, if this is something that was previously just a local variable this is going to force it onto the stack just so that it can be passed as a reference to the called function. ABIs difffer a fair bit it's not just size that impacts things but also if the type is trivial, if the ABI splits structs (like System V), then you have some which don't have many registers for arguments like MS ABI
      Also 32-bit x86 is pretty much dead these days for any game you ship and not worth worrying about, everything targets 64-bit now.

    • @malckhazarsteamcat9817
      @malckhazarsteamcat9817 Před rokem

      @@user-cy1rm5vb7i You can break ABI only if you write the call by hands in Assembly language. ABI is the convention which is followed by compiler, not the developer.

    • @anon_y_mousse
      @anon_y_mousse Před rokem

      @@malckhazarsteamcat9817 You mean the platform. The compiler just targets the platform, so if the convention used is wrong it's because the compiler targeted the platform incorrectly.

    • @malckhazarsteamcat9817
      @malckhazarsteamcat9817 Před rokem

      @@anon_y_mousse No, you mistook.
      ABI (or Application Binary Interface) is a convention declaring the order how calls should be made in Assembly. It is included in that you call a "platform", but, technically, it does not restrict you to use other orders of calls inside your code. The problem will arise only after you violate ABI used in compilation of other binary parts of your app - libraries, system calls, etc.
      Inside your code no one can restrict you from writing the call to your function in Assembly.

  • @StevenMartinGuitar
    @StevenMartinGuitar Před rokem

    yep split the reviews up!

  • @__hannibaalbarca__
    @__hannibaalbarca__ Před rokem

    Also shallow copy have very interesting thing to do with.

  • @suvalaki
    @suvalaki Před rokem

    Hey for preserving maintainable code going forward are you suggesting some static analysis as part of your Ci pipeline to determine when if a struct grows in attributes over time? Allowing for pass by value for primitives makes perfect sense but I worry that as newer developers modify code you are just asking to introduce issues? Sort of unintended side effects from modifying the base object

  • @TheBestNameEverMade
    @TheBestNameEverMade Před rokem

    So an aspect that was forgotten here was the speed of the compiler. In large project or large engines it's very important to forward declare things. So if it's a large file that gets pulled to include a variable in an it's not the hotpath seriously consider using a reference so the type can be forwarded declared instead... of course if you are not working with code that is likely to get very big don't worry o much.
    However c++ compilers can get very slow very quickly if everything is being included in headers which will mean less time for the dev to spend optimizing code that matters.

  • @mikefochtman7164
    @mikefochtman7164 Před rokem

    Regarding monolithic code reviews vs snippets about specific coding issues, I vote for the latter. Bits of info about specific topics seems better (IMHO).

  • @dainxor
    @dainxor Před rokem

    Please do the small talks about this topics 🥺🙏

  • @Firepipproductions
    @Firepipproductions Před rokem +1

    I would pass a custom struct of two 32 bit ints but not a 64 bit int as const ref. Conceptually you are right about the pointer argument, but especially if you make it const, the compiler figures it out to be the same. The custom struct might be 8 bytes now, but could be expanded. It's also just more consistent to have built in types by value and custom stuff const ref. If you use that pattern, passing a custom small type by value will look jarring.
    Also, as you mentioned yourself, const ref can take an rvalue. This means it doesn't actually have to treat the value as a pointer. Does 2 have a memory location? Not until the compiler decides what to do with it. In fact, const ref tells the compiler to do what is most optimal actually. It might just in place the argument as value wherever it is used for instance.

  • @arsnakehert
    @arsnakehert Před rokem

    Oh no, watching The Cherno's videos gets me excited about copying memory and pointers and whatnot in C++, I don't want to dive into this rabbit hole again *starts editing makefile* oooooooh nooooooooooo what have you dooooooooooone Chernoooooooooooo

  • @entusiast2000
    @entusiast2000 Před rokem

    AFAIR, x64 compilers like to use __fastcall-based calling convention. In this case **maybe** there's no difference in **memory** used for the value/address transfer (if the value's trivial, less than a register width etc etc..).

  • @dnd3347
    @dnd3347 Před rokem

    Wow! Finally my decade long question has been answered :-)

  • @jenselstner5527
    @jenselstner5527 Před rokem

    As pointers of course! ;o) 😄

  • @ayushanand18
    @ayushanand18 Před 9 měsíci

    Man you are God to me!

  • @art0nsec
    @art0nsec Před rokem

    I prefer to watch the code review split into smaller topics. Makes it easier to process and I search it, should I ever want to rewatch it.

  • @danielhalachev4714
    @danielhalachev4714 Před rokem

    I almost always used constant reference without thinking, because I thought the compiler optimised it.

  • @monstraxis7365
    @monstraxis7365 Před rokem

    maybe you could divide the video by topics, but also upload the full one later. I actually enjoy watching the full video

  • @linghaozhou4695
    @linghaozhou4695 Před rokem

    I got a question regarding the struct where you showed the deepcopy with dynamic memory allocation, does that need to dealt with delete[] somehow later?

  • @dune2themaker
    @dune2themaker Před rokem

    Yes good idea to split!!

  • @chikwendumcdonald1581

    Yes, it's a good idea to split the video into topics.

  • @sanyi_derda
    @sanyi_derda Před rokem

    9:45 like the new idea :)

  • @openroomxyz
    @openroomxyz Před rokem

    Thanks, yes better format.