This channel is all about code. After more than a decade-long career in programming and designing software and a PhD in robotics and computer vision, I decided to have my own place on the Internet where I can chat about things I encountered throughout the years. The content will have more or less rigid courses on that or another programming language or technique, but might eventually morph into something less structured. Only time will tell. For now - welcome to this new journey and hope you will enjoy it with me!
Буду откровенен, пересматривал 4 раза, и все рано не во всем разобрался. Так и не понял, для чего нам сохранять переменные с каким-то текстом. Однако, отличное видео - нужно просто приложить немного усилий. Мне, кажется, ролика хватит, чтобы понимать 90% cmake файлов. Все четко, и без воды
Дякую за хороші відгуки! Стосовно змінних, загалом всі змінні в cmake це просто текст. І більшість цього тексту зберігається в кеші який потрібен для того щоб зберігати конфігурацію між запусками. А все інше про за я розказую то наслідки цього.
@@CodeForYourself Да, только момент с кешем не уловил. Все остальное кристально. Особенно понравилась презентация на гитхабе. Я вот раньше просто смотрел, ничего не повторяя. Сейчас Вы сказали "не тупить" и выполнить все самостоятельно. Начал это практиковать. Времени намного больше занимает, но после ощущение будто сам все сделал. А если вопросы есть, гпт использую. Еще раз спасибо :-). Подписка, лайк
@ozimandias1858 будьте обережні з гпт, адже він хронічно бреше. Будь які конкретні запитання можна також задавати тут або на гітхабі. Я відповідаю повільніше за гпт, але сподіваюсь що менше брешу 😅
Thanks for the compliments! Whenever you see code animations I use motioncanvas.io The rest of the animations, including the last diagram animation here about shared pointers, I make by creating images with the Apple's Freeform, import them into Davinci Resolve where I edit my videos, and do the rest of the animations there.
That is it make sure that when we call these functions we look up the actual functions to be called in the vtable. In the end of the example we pass a reference to an *IoInterface* object. So try to make sure you understand exactly what will happen when we call *Write* on such an object reference. Does this help?
@@CodeForYourself I understand what you mean but in the video you used "void Write(const std::filesystem::path& path, const std::vector<Color>& data) const override { std::cout << "Writing PNG to path: " << path << std::endl; }" function but on github you used "virtual void Write(const std::filesystem::path& path, const std::vector<Color>& data) const override { std::cout << "Writing PNG to path: " << path << std::endl; }" function for both png and jpeg. I expected and understood first function but did not understand second one.
@@erkamkocaer2097 oh! Now I understand! Thanks for pointing it out! It's a bug I have on GitHub. We *can* have *virtual* and *override* for the same functions but generally we should just use *override* as the *virtual* is implied here. So basically what I'm trying to say is that *override* implies *virtual*. I'll remove *virtual* from my GitHub code as it was probably a copy-paste error on my side. Thanks again!
I’ve been watching your channel and following along from the start. This video just helped me tie a lot of concepts together. Many thanks, keep it coming!
why do a video? why not simple write a nicely written article that people can read at their own leisure, and one where you can correct the numerous issues raised on the cpp subedit
That is a valid question. The main reason is that some people prefer a video format and a video allows me to do both, have a markdown article and a video. You can find the markdown under the video. The downside is of course that errors are harder to fix.
@@richardbennett4365 Same explanation as for the first change. The blank T-shirt is the original recording and the “save the earth” one is the follow-up 😅
@@richardbennett4365 simple. I recorded everything and then, when editing found that the story wouldn’t add up. So had to re-record and add a part. But the old tshirt was already in the laundry basket 😅
About the manual delete, I always get questions from friends about whether they should use delete on a raw pointer they got as a return value from a function inside some old C library, It is all super confusing for beginners. Usually Documentation have it mentioned as "non-owning". It's amazing to see how all over the place each library's cleanup methods are.
It's great that people got interested by memory management, but I fully disagree with the usage of smart pointers and garbage collectors as a reliable and performant way of managing memory. Those techniques assumes that every "object" has his own lifetime, witch is fundamentally false: When there's one, there's many. This affects performance because of memory fragmentation and loses capabilities. I highly recommend the talk of Ryan Fleury about Arena Allocators, it's mindblowing how simple and effective a linear allocator can be. I almost never think about memory management and have all the performance and reliability.
Arena allocators definitely have their use! However, if we allocate a lot of objects we will quickly need to manage memory in complex ways. This lecture targets largely the default behavior for beginners. I believe that using the heap for allocations should be completely fine in most domains. Also, heap implementations benefit from years of optimization and as far as I know they basically use arena like allocations for small objects. That being said, if one really writes a safety critical application and cannot use heap memory at runtime then yeah, we’ll probably need to allocate stuff from an arena. Also, I don’t agree with putting garbage collection and smart pointers in the same row. They are very different mechanisms. Smart pointers are definitely reliable and in case of a unique pointer have nearly no overhead. So if one wants to allocate on a heap, smart pointers are definitely a way to go.
Very Nice lecture, all my years I have always been a Composition over Inheritance supporter as well I knew that private inheritance can get the job done but never bothered to see how; example at 9:59 cleared the syntax and semantics up perfectly. ☺
Nah man, RAII is there for a reason. There is no free lunch. It's an indirection that in many cases isn't necessary. Writing a quick free with a null check into a destructor is not hard. Smart pointers are great for some things, but a shared pointer is inefficient and a unique pointer is kind of obsolete in many cases. I think nuance would make this a bit better. They should not be your default move. You should always evaluate your options.
How is Unique pointer obsolete? it executes the same instructions as Raw pointer, same size as well, Scott Myers even recommended them to be used in tight cycles. Shared pointer though not to be confused with Auto GC is still a deterministic destructor so plenty useful, the only inefficient thing I can recall about Shared pointer is that it is twice the size because of control block pointer.
@@segfault4568 I have a similar view (thus my original question to the OP). As for the shared pointer the reference count is atomic if I remember correctly, which requires synchronization and therefore might cost additional resources and instructions.
@@Pedro-jj7gp that’s just the way it usually is on most modern systems: any pointer is 8 bytes. In the end this has to do with being able to address enough places in the memory if this makes any sense. That being said, other systems could theoretically have it be smaller or larger. Does this answer your question?
@@CodeForYourself It does, but I'm a bit confused as I was watching some other videos about vtables and they showed the vpointer taking up 4 bytes... I guess it could just be on that system.
@@Pedro-jj7gp it might be. But you can always check it on your system too. I’ve only ever seen 8-byte pointers on every system apart from really non-standard embedded boards. 🤷♂️
@@CodeForYourself Yup, you're right! I just checked using sizeof() both for a regular int* and a vpointer using reinterpret_cast<int*>() and both are 8 bytes. The video in question was 12 years old, maybe it was a 32-bit machine
Nice and clean overview. However, dynamic polymorphism is a very well known and old topic in C++. I would suggest deepening into static polymorphism for the next video. Keep it up, it is refreshing to see technical C++ stuff in RU-vid 2024
@@SystemSigma_ thanks! I actually put static polymorphism before this video, it’s right along a series of videos about templates: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-1Mrt1NM3KnI.html Still not the most modern thing but necessary to understand concepts down the line I believe. I also wanted to give an overview eventually of how to do the same things using both dynamic and static polymorphism on some concrete examples. But for that people need to be familiar with both. Does this make any sense?
@@CodeForYourself It does indeed. I meant that modern c++ features (especially type traits) are really useful for making more robust static polymorphic code. Maybe instead of going too generic with topics, restricting the features scope may be more interesting for experienced viewers.
@@SystemSigma_ I agree that a more in-depth look would suit experienced folk better. My aim here, however, is to fill in the complete playlist that would tailor to somebody who knows nothing about C++ and guides them gently through all the main topics, providing the "why" for using what they learn along with enough information to dig deeper. Please see here for the full list to date: github.com/cpp-for-yourself/lectures-and-homeworks Maybe one day, I will finish the basic course and have some time to dig more in-depth into certain things. That being said, I feel that there is plenty of that type of content on RU-vid already, while the comprehensive courses that I've seen all fell a bit short of what I wanted to achieve here. But we'll see if my course is going to be helpful to anybody 😅
@@SystemSigma_ oh, and just in case you have not seen already, I _do_ talk about type traits a bit more in-depth here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-IQ62tA51Vag.html
Hi. I think I have the C++ part of this completed, and would like to submit it for testing. But I'm a bit lost on the git side of this. I tried to use the same git template instance, and put my fortune_celler.cpp in ~/src/Full-C++17-course/t/cpp-for-yourself/homeworks/fortune_teller/task in a fresh branch (I also tried it in the same branch), but upon push'ing that to origin, I just got the example_homework graded again. Any suggestions?
@@McDonaldIbekwe ok, so there is one video on testing that I did before but it is not in-depth. As for coroutines I would have to use them in some real projects to be able to speak confidently about best practices that relate to them unfortunately. 🤷♂️ I will try to squeeze some multithreading in though at some point but I can’t say when just now 🤷♂️
Yes, you are totally right, but I much prefer to be explicit rather than implicit. My logic is that it is not a big deal to type one more word but it allows us to not think about which inheritance is going to be picked by the compiler implicitly should we omit that “public”. Does this logic make sense?
Дякую! Радий, що сподобалось! Подивимось, взагалі план був про це розказати, але це така велика тема і в ній так багато чого відбувається, що я не впевнений що зможу її просто і добре пояснити. Але вона у мене в планах. 👍
Yeah, I thought of fitting to the standard UML style but then left it at that considering that the tools I used for creating the visuals did not have a non-filled arrow 😅
SPOILER: Wrapping everything up with the Image class example from the start was a very nice move 👍 I really enjoyed this lecture and will definitely recommend this further. Thanks a lot for your awesome content! 👏
Thanks! I’m really happy that that move clicked. It was a long video so I wanted to end at one consistent example that would be relatively close to real life. 🙏
This video took A LOT of time to make. Just the script I had to rewrite like 3 times. So, if you like what you see, please share this video with your C++ inclined friends if you found this video useful. 🙏 And do leave a comment, this way this video will be shown to more people apparently 🤷😉
I wonder how the templates are used in data structures in STL (for ex: vector) so that it not only works with primitive data types like int, float,etc. (for which can do the template specialization) but also with user defined classes/structs, which is not known before hand. Any ideas ? @CodeForYourself
@@UvUtkarsh generally the code is written in such a way that as long as the provided types follow some form of an interface they should work. They also use traits and partially specialize classes based on what those traits tell about the provided type. I might do an in-depth look into this later if there is interest
@@isfandyar3937 also, I cover this extensively in this video: Why use templates in modern C++ ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-1Mrt1NM3KnI.html
If I'm not mistaken, this only helps speeding up compilation times due to the explicit template specialisation. Still, what other benefits do I get by splitting template declarations and definitions?
I guess you're right to a degree but there is more to it. Explicit template instantiation (different from specialization) does indeed help the compile times. It helps because without it, the code would live in header files and so would be copied to any translation unit that includes that header, leading to the compiler needing to compile all of those instances on their own. If we put the code into a source file (and for that we need an explicit template instantiation) we will only compile the code once and link it to the places where it is used instead. Does this make any sense? Now as to other benefits, one big downside of header-only libraries, apart from compilation times, is that all the implementation actually lives within the header files. Meaning that if we want to distribute our library to other people we essentially have to distribute source code. So in the end, we have a sort of a tradeoff, header-only libraries are quite simple to use but might lead to long compile times as well as to needing to show our source code to anybody using our library. I talk about this at length in the video about libraries here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Lxo8ftglwXE.htmlsi=JYNvd_2i6GLjfdvv
I'm perfectly aware of the standard benefits of splitting declaration and definition.. Still, for templates I really don't think it's worth the typing effort (if you're outside a smart ide) 😅 I guess the most valid point is only hiding the source code for distribution 😊
@SystemSigma_ sometimes the compile times are important too. Some years ago I added a couple of explicit instantiations in a code base to save about an hour compile time 😅 I think if we *know* the types we’re about to use, then I would go for a compiled library as opposed to a header only one. It works also as an additional check for my design to a degree 🤷♂️
Great video! One thing that bothered me a bit was usage of "<typename ClassT> to specify template parameter. The parameter has nothing to do with class, it could be just as easily called <typename T>, the word "class" gives a wrong impression, at least to me. Thanks!
Ok, I agree with you that ClassT doesn’t hit the mark. What I prefer doing generally (when I cannot use concepts) is to give the template parameters meaningful names. Considering that this function is very illustrative and doesn’t really have a proper purpose it is hard to pick such a template parameter name, meaning the one that actually makes sense. So here I picked the ClassT as a substitute but I agree that it is a bit stupid and if I would have thought longer about it I probably would have just used T here. Thanks for your comment!
Unfortunately the answer to this comment is more complicated than I would like it to be. It has to do with how overload resolution is taken care of in C++, which is not trivial: en.cppreference.com/w/cpp/language/overload_resolution But the rule of thumb, at least the way I think about it, is that if we have a local object the compiler will prefer passing it by reference if an appropriate function overload exists. During this some implicit conversions can take place as well as some copies performed. My recommendation would be to write a simple example with some Foo function that has various overloads and see what the compiler does in each case to build more intuition. That being said if somebody who can explain it better stumbles upon this comment, I would like to find a nice and simple explanation for the future. 😅
@@CodeForYourself thank you, i really appreciate the answer, it surely provides a good starting point for me to try and understand this :) Keep up the great work, i discovered your channel with this video, and decided to start the whole course from the first lesson, even if I'm familiar with some of the basics of C++
@@francescobenacci6611 thanks for the kind words! Please mind that the first videos have a slightly different style from this one. I guess I converged to a style like this some time around the cmake video 😅