I am adding cpp file as include. Is this bad approach?
@CodeForYourself3 күн бұрын
There are some obscure situations when people do that but it is always a bit out of the ordinary. What are you trying to achieve by including the cpp file?
@zamf2 күн бұрын
If you're not sure, then yes. It is a bad approach. You have to have a pretty good reason for including cpp files.
@CodeForYourself2 күн бұрын
Yes, actually this makes sense. While there might be situations when it might be helpful, none of those are for beginners and if one has to ask, probably it can be avoided. 👍
@secondbest71872 күн бұрын
@@CodeForYourself Thanks. Actually, I am using it for class template like below. template <class T> class AAA { }; It is working on cortex-m mcu and I need this class template. It is a requirement. I am not able to compile code without using #include "AAA.cpp". When I watch your video, it is touching exactly what I am doing. There are not many engineers using cpp on embedded sw engineers. So, I want to ask you. I will also try your method. Thanks.
@CodeForYourself2 күн бұрын
@@secondbest7187 you should be able to compile your code without including a cpp file, that’s for sure. Everything described in the video is defined in standard and will work for any compliant compiler and processor.
@SystemSigma_3 күн бұрын
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?
@CodeForYourself3 күн бұрын
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: kzfaq.info/get/bejne/gt6fa8mlzNLbiHk.htmlsi=JYNvd_2i6GLjfdvv
@SystemSigma_3 күн бұрын
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 😊
@CodeForYourself3 күн бұрын
@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 🤷♂️
@SystemSigma_3 күн бұрын
@CodeForYourself yeah, I used it with quite a success in a few embedded projects, but, of course, every project is different so 🥲
@CodeForYourself3 күн бұрын
@@SystemSigma_ yeah, I agree. 🙂↕️
@CodeForYourself3 күн бұрын
Thanks a lot for watching, folks! Don't forget to share any thoughts you might have on this (or anything else)! 🙏
@yousefsaddeek17 күн бұрын
CMake Deez nuts
@Maximus9824518 күн бұрын
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!
@CodeForYourself18 күн бұрын
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!
@francescobenacci661120 күн бұрын
@10:57 when we call DoSmth(number) i didn't expect the parameter type to be int&. Why is that, and not simply int? After all, it will be copied...
@CodeForYourself20 күн бұрын
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. 😅
@francescobenacci661120 күн бұрын
@@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++
@CodeForYourself20 күн бұрын
@@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 😅
@segfault456821 күн бұрын
Universal Reference gang member Here, Scott Myers most loyal soldier.
@CodeForYourself21 күн бұрын
Welcome 😅
@zamf25 күн бұрын
An interesting fact is that forwarding (or universal) references were discovered and not designed in the language. They just happened to spring into existence following the rules of template deduction and reference collapsing. It's one of the many miracles of the language showing that when you design something right it gives more value than designer has originally anticipated. It's an example of 1+1 giving more than 2.
@CodeForYourself25 күн бұрын
Yes, thanks for this comment! I believe the reason the name “universal reference” exists is just for that reason too! It came before the “forwarding reference” name was used in the standard. That being said, I don’t have the hard facts for this, so if somebody can point me in the right direction I would be immensely grateful 😉
@McDonaldIbekwe26 күн бұрын
Great Video. In the future would love to see Map with Object of class as key and as value. Love the music at the end. Please, can I get the full song?
@CodeForYourself26 күн бұрын
Thanks 🙏 I’ll note the interest and we will see if I get to record something more about the map class. As for the music it is one of the KZfaq samples. I’ll try to find the actual name tomorrow and send it your way.
@woddenhorse26 күн бұрын
I thought you'd say FetchContent, but submodule it is 🥳🥳
@ilovenirajanАй бұрын
still can't figure out on what paramterts to put to make it choose this function T&& forward(std::remove_reference_t<T>&& t) instead
@CodeForYourselfАй бұрын
Did you try the ones at the end of the lecture? Like pass a real temporary value into the forward function directly?
@ilovenirajanАй бұрын
@@CodeForYourself oh lol, my bad. I mistakenly put real temporary value into doSmth instead of forward. lol, i'm so stupid. sorry for bothering u.
@CodeForYourselfАй бұрын
@ilovenirajan no worries at all! This is what learning is all about 👌 I make these mistakes all the time!
@en_xАй бұрын
I was not able to replicate the uninitialiazed variable problem in @22:00 it always showed 0.0000e+00
@CodeForYourselfАй бұрын
That is expected and is kind of the point that I was trying to make! Undefined behavior is like that: one cannot rely on it failing reliably. It will sometimes fail but then will also not fail at other time. Which makes it really frustrating 😬
@en_xАй бұрын
@@CodeForYourself Thank you very much, Yes, I understand your point. I learned this the hard way once and now I have this uncontrollable urge of explicitly initializing everything (also usually this is the first thing I check when debugging) .
@CodeForYourselfАй бұрын
@en_x same on my side. It really pays great dividends! 😉
@WizarthАй бұрын
Thank you for walking through how this forwarding works, particularly the ampersand collapsing. I hadn't fully understood how forwarding works until you put it all together like that.
@CodeForYourselfАй бұрын
Thanks for the kind words! I have to admit that before recording the video I didn’t understand it fully either 😅 Well, I had a pretty good idea but then my intuition was wrong with respect to which forward overload would be called. 😅
@AzerAnimationsАй бұрын
You have some of the best videos on C++. I would love if after your "C++ basics" lessons that are happening, you could move to a more specialized subject. I see you have a PhD in computer vision, so that would be a cool subject to tackle in C++ (as a lot of tutorials use Python)
@CodeForYourselfАй бұрын
Thanks 🙏 I keep playing with this idea. I come from a robotics background and we’ll see if I manage to make something out of it once I finish the C++ course here 😅
@JkaBGАй бұрын
I find it better to understand if I read `&&` as `owned` or `owned_value`. #define owned && So the difference between `int` and `int owned` is the ownership.
@CodeForYourselfАй бұрын
That’s an interesting idea, although I don’t like the use of macros here (what if you have a variable called owned in the code or smth) But apart from that it sounds fun. I’m not sure how I feel about it just yet as in my head a cope of a variable owns its memory too and can transfer it if we pass it through an std::move and some move assignment operator 🤔
@JkaBGАй бұрын
@@CodeForYourself Yes, I do not use in code but for explaining and understanding it is good. You can show a `int &&` and then show `int owned` to compare it in different contexed. For example: Saying `int &&` / `int owned` has more in common with `int`, than `int &`. Comparing && and & can be very confusing.
@CodeForYourselfАй бұрын
Hey everyone. How did you find this video? Too long? Or short? Too complicated? Or rather too dumbed down? I need your feedback to make sure I’m getting better. So… what do you think? 🙏
@WizarthАй бұрын
I don't know any way you could cover this topic with sufficient detail and be any shorter or less complicated. For me, it was just right.
@CodeForYourselfАй бұрын
Awesome! Thanks for the feedback and glad you liked it! 🙏
@taschenorakelАй бұрын
You should give more time to read and understand the examples. It's really nice to see you talking, but often you return from code to yourself way too quickly.
@syyyr29 күн бұрын
I don't have much use for std::forward (and had no idea how it works, and when to use it), so for me, it had the correct amount of detail.
@CodeForYourself29 күн бұрын
@@taschenorakel totally makes sense. In this format I made a deliberate decision of keeping up with the pace to keep people a bit more engaged. My reasoning was that if people would really want to return and follow all the details they can always pause the video. I might convert all of this into a proper course on a different platform geared for just learning where I could maybe reduce rhetoric pace somewhat 🤔 Thoughts?
@chege54Ай бұрын
My bet is: //2 //1 //3 //4 and 0 2 1 1
@samuelzev4076Ай бұрын
Can i also ask HTML or is it only C++?
@CodeForYourselfАй бұрын
I’m not an expect in web dev, sorry 🤷♂️😁 For now it’s just C++. But once I’m done with the course we’ll see what comes next. That being said, it’s more likely to be Rust rather than anything web oriented 🤷♂️
@D1eg0_Ай бұрын
VERY USEFULL!!! thanks for making learning fun!
@CodeForYourselfАй бұрын
Glad you like it! 🙏
@D1eg0_Ай бұрын
@@CodeForYourself im exploring your vids and they are really cool 😎💪
@CodeForYourselfАй бұрын
@D1eg0_ really glad to hear that! Don’t forget to tell your friends! 😬
@ChimiChanga1337Ай бұрын
Hi, I seem to forget small details like flags -c to not use linker etc. How to remember them?
@CodeForYourselfАй бұрын
I wouldn’t bother. It’s really not such a big deal. One usually needs those flags only to figure out some weird thing happening. Once you see the linker errors you know you forgot the -c flag 😁
@ChimiChanga1337Ай бұрын
What if we make header-only libraries instead.
@CodeForYourselfАй бұрын
You mean like not ever use any other libraries?
@ChimiChanga1337Ай бұрын
@@CodeForYourself okay I'm too much of a beginner. I think I understand the tradeoffs of different types of libraries. Header only libs makes the devs life simple. But if you want to hide the implementation then they wont work.
@CodeForYourselfАй бұрын
@@ChimiChanga1337 yes, you are exactly right! Also, header only libraries, when we have many of them, make compilation waaay too slow. With compiler libraries we are able to often compile them once and link all over the place. Does this make sense?
@ChimiChanga1337Ай бұрын
@@CodeForYourself Yes, I understand now. I really want to thank you again for this course!
@CodeForYourselfАй бұрын
@@ChimiChanga1337 my pleasure and I'm really happy that you like it! 🙏
@pitri_hubАй бұрын
Template meta programming with (partial) specialization is such a wildly different language than to "regular" C++. And it's such an incredibly deep rabbit hole. Interestingly enough, my knowledge about this topic really helped to understand Haskell a lot better during the semester it has been taught in. The signature pattern matching in partial specializations is almost identical to how Haskell functions are overloaded for their use cases. C++ really helps to become a better programmer, because it exposes the user to a lot more concepts than certain other languages that deem certain mechanisms to low-level or dangerous to be exposed to the programmer. But seeing how C++ was helpful for such an unrelated language with very different paradigms was just wild. :D
@CodeForYourselfАй бұрын
Thanks for sharing! Did not think of this parallel! I have only played a little with functional language but yes, the recursiveness of template meta programming I guess is what makes the experiences similar.
@harleyspeedthrust401323 күн бұрын
I came back to c++ after spending some time with ocaml, rust, and idris, and everything was so much easier. This template stuff makes a lot more sense now and these videos are really good
@CodeForYourself23 күн бұрын
@harleyspeedthrust4013 thank you so much! Hope you keep enjoying the videos coming next! 🙌
@DimaLyganovАй бұрын
Nice T-short, man!
@CodeForYourselfАй бұрын
Thanks, Dima 😉
@gurumahendrakar65Ай бұрын
Sir. You Can Make A Video C++ Virtual Tabes Dynamic Linking How Works. With assembly.
@CodeForYourselfАй бұрын
Not sure I want to include assembly in that video but otherwise the vtable video is planned already! 😉
@gurumahendrakar65Ай бұрын
@@CodeForYourself Just Waiting 🙋
@CodeForYourselfАй бұрын
Will try my best to make it as soon as I can 👌
@ChimiChanga1337Ай бұрын
BRUH. The universe might actually be alive. I have been struggling to understand templates and was a bit sad that you didn't drop a video on them. Now here you are. FTW.
@CodeForYourselfАй бұрын
Hope it helps 😅
@tienvuhong35362 ай бұрын
That pretty much helpful and so easy to understand, thank you!
@ElMagic642 ай бұрын
Cmake is absolute dogshit
@CodeForYourselfАй бұрын
While it has its quirks, this is probably too strong of a statement 🤷♂️
@silentage63102 ай бұрын
Does this code at the end of each test run attempt to update submodules? Will this slow down the testing process if tests are run frequently?
@CodeForYourself2 ай бұрын
Thanks for the question! The update code is only run at the cmake configuration stage. So it is not run when the tests are run. Does this make sense?
@whac-a-robot2 ай бұрын
Hi Igor, I am using CMAKE 3.16, when I tried the above I am getting a message stating that no tests found. How to resolve this?
@CodeForYourself2 ай бұрын
Yeah, that’s a bit of a limitation. There was another comment under this video where we come up with a solution. Does that solution work for you?
@whac-a-robot2 ай бұрын
@@CodeForYourself where is that test folder is mapped? I couldn't find it.
@CodeForYourself2 ай бұрын
@whac-a-robot8623 it should be just the build folder itself. So you have to change the directory into that build folder and run the cmake command from there. If that doesn’t work out I’ll have a look more deeply into this.
@whac-a-robot2 ай бұрын
@@CodeForYourself ok will check and let you know
@whac-a-robot2 ай бұрын
@@CodeForYourself I understood it now. For quick reference for others you putting my solution here. Command 1 : cd build . Command 2 : GTEST_COLOR=1 ctest --output-on-failure -j12. Thank you for the support.
@Awesomekid22832 ай бұрын
Really great video and I love how much you went through in such a short amount of time. I was messing around with code to produce the two problems with template specializations and I found some interesting behavior I was wondering if you could shed some light on. For the record, I was using C++23 and Clang. The code I tested first for the problem that template's will never match reference types unless they are explicitely specified worked exactly the way you showed. However, I tried the Dimov/Abrahams problem using an Object pointer instead of an int pointer becuase I had created an object class for testing the previous example. Everything worked like I observed in your video until I tried changing the specialization to a function overload. My code still picked the template-function overload instead of the function overload! This was really confusing to me since I thought regular functions were always preferred over templates. I even tried moving the functions around, but no order I put them in changed the result. This stumpted me to no end, I tried the same example with an int and it worked just like you said it would! It seems the rules for templates are different for object pointers for some reason? I then thought that maybe the fact that I had these functions in a header file I was including was contributing to the problem because of course they were marked inline when necessary, so I tried in my main file and I got exactly the same behavior. This actually only ended up confusing me more because I encountered more strange behavior I wasn't expecting regarding the namespace from my header file. As usual, I put the namespace name in front of all my function calls since I didn't declare that I was using that namespace. CLion said that the namespace name wasn't necessary on the function calls where I specifcally passed an object or the address of an object, but it was necessary on my function calls where I was passing in an int or the address of an int. I then tried running my code and lo and behold I got errors when removing the namespace on my primitive function calls but I didn't when I did it for my object calls! I have absolutely no idea what was going on here and I would love an answer. It got weirder yet again because I had functions with the same name in both my main file and my header file, but I didn't get an error when omiting the namespace specifically on the calls where I was passing an object or the address of an object, which was strange because I thought there would have been a name conflict, but even worse, C++ actually picked the function from the header file in a different namespace instead of the one in the current namespace! I've seen some bizarre things in C++, but this is one of the most bizarre and I'm completely lost. I had to use the global-namespace specifier to get C++ to choose the function from it's current namespace which didn't make any sense. Even weirder still, this was only a problem when I called the function with an object. When I called the function with the object's address, the correct one was picked! I know you're probably busy, but I would love it if you could take the time to answer or direct me to a video explaining it if you know of one! Here is the link: godbolt.org/z/sdGsWra8G.
@CodeForYourself2 ай бұрын
Sounds interesting. I’d like to give it a go. Can I ask you to setup a small reproducer on compiler explorer and share the link here?
@Awesomekid22832 ай бұрын
@@CodeForYourself I just realized today that KZfaq deleted my reply because I guess it thought the link was spam. I'll try to do my best to get it to go through. I can't give you the full link, but if you go to the normal url for Compiler Explorer, it's a '/z/' then sdGsWra8G. Let me know if that works! Thank you for taking the time!
@Awesomekid22832 ай бұрын
@@CodeForYourself I'm sorry it took me so long to respond! I thought I had, but it seems KZfaq won't let me reply with the link. It actually kept deleting my reply! I just edited my original comment to include the link at the bottom. Thank you, again, for taking the time!
@CodeForYourself2 ай бұрын
@@Awesomekid2283 thanks a lot. Got it. Will have a look soon!
@CodeForYourself2 ай бұрын
@@Awesomekid2283 Ok, I've looked into your example and see a couple of issues that could cause the behavior you describe. First of all, your functions are all `inline` in the header. Which means that they already have all of their definitions. You don't need to (and generally cannot, but see the next issue point) redefine such `inline` functions in the cpp file. You are only able to do it because of the namespaces that you use. Which brings us to the second point. Second, you have different namespaces in the header file and the source file. All of the functions in the header file are in the `templates` namespace, while all of the functions in the cpp file are in the global namespace. Now what happens is this: the functions in the header file are fully defined. The functions in the cpp file *do not define previously declared functions, they define new functions in the global namespace*. This causes all of the issues that you experience. Does this make sense? If you try to add the namespace `templates` in your cpp file, you will get quite a few "redefinition" errors during the compilation. I would generally recommend to try to simplify your testing setups. I do it myself. If I want to make sure that I understand a certain concept, like template specialization, I build up a *minimal* example - as small as I can, to make sure I'm only testing what I wanted to. In your case, you have at least three things going on in the same time - header/source file separation, namespaces and then template specialization/overloading. Separating these into separate small examples lets us learn them more easily.
@KotlasBoy3 ай бұрын
Great gratitude from 1 course student!
@CodeForYourself2 ай бұрын
Thanks for sticking around! 🙏
@andreacassotti25613 ай бұрын
Thanks a lot for your tutorial! It's very clear.
@CodeForYourself3 ай бұрын
Thanks for watching and for the kind words!
@rodneydumenu43293 ай бұрын
Tenary operator is challenging for me.
@CodeForYourself3 ай бұрын
What is it that confuses you with it?
@rodneydumenu43293 ай бұрын
@@CodeForYourself I don't understand the syntax. say, z=(a>b)? a:b does it mean if a>b is true, z should store the value of a?
@CodeForYourself3 ай бұрын
@@rodneydumenu4329yes, you’re right! I should probably add this example to the lecture at 2:59 🤔 Not only will it call the appropriate statement based on the boolean value on the left but it will return the result of that statement.
@Regis-03 ай бұрын
Why can't I subscribe second or third time 😅 Great video!
@CodeForYourself3 ай бұрын
Thanks! 🙏 That would’ve been great 😊
@heel573 ай бұрын
Great video - thanks. But - the manual editing of the CMakeCache.txt appears to break what I have learned: "don't modify automatically generated files". Why does this not apply here?
@CodeForYourself3 ай бұрын
Hey there. That’s a very good point. I’m not sure exactly what to say here. My best guess is that this mechanism in cmake exists longer than the principle you quote. That being said, even though the cmake cache was seemingly designed to be edited by hand, I don’t remember the last time I had to do that.
@heel573 ай бұрын
@@CodeForYourself Ok thanks for taking time to answer my question. I am relieved by your answer that it is not a necessary / common thing to do. I could imagine it to be relevant to temporarily try something out - but the change is gone without warning on next cmake.
@CodeForYourself3 ай бұрын
@@heel57yeah. It is mostly a convenient way to explain why it is there in the first place. I found no other good way to explain cmake cache in an intuitive way.
@dino_source3 ай бұрын
I am a simple man. I see a new video from `Code for yourself`, I hit the thumb up button.
@CodeForYourself3 ай бұрын
Thanks a bunch! 🙏
@yurkoflisk3 ай бұрын
19:21 The explicit specialization is called in this case. As you mentioned, overload resolution, irrespective of where (and whether) the explicit specialization is declared, chooses `void Process(T*)` as a better match when calling `Process(pointer)`. Now, when the explicit specialization is declared *above* `void Process(T*)` it specializes `void Process(T)`, so the chosen template `void Process(T*)` doesn't have any explicit specializations and hence is (implicitly) instantiated from the template itself to form the (instantiated) specialization `void Process<int>(int*)`. When the explicit specialization is declared *below*, though, both templates are seen from it and both can deduce their T from the specialization, thus the ambiguity of which of the templates is actually being specialized is, per [temp.deduct.decl#2], resolved by partial ordering defined in [temp.func.order] and [temp.deduct.partial], i.e., a _more-specialized_ function template is preferred. In our case, `void Process(T*)` is more specialized than `void Process(T)` because `T` in `void Process(T)` can be deduced from `U1*` in `void Process(U1*)` while `T` in `void Process(T*)` can't be deduced from `U2` in `void Process(U2)`, where U1 and U2 are some unique synthesized types. Thus, this time it is `void Process(T*)` that is being specialized by our explicit specialization (resulting in specialization `void Process<int>(int*)` being explicit, not instantiated), so when `void Process(T*)` is chosen by overload resolution, its specialization is defined as we intended.
@CodeForYourself3 ай бұрын
Yep, you’re totally right! Thanks for answering 🙌
@GoWithAndy-cp8tz4 ай бұрын
I wonder why statically typed language like C++ is so messed up with unnecessary features just because to look like dynamically typed ... What is the gain of templates because not readability and clearness for sure? Cheers!
@CodeForYourself4 ай бұрын
That’s a good question. There are multiple reasons why C++ is so complicated. The main one is that it is a language that has been in active use for around half a century. Just let that achievement sink in. At the same time it manages to be quite modern as languages go today. Now to make it be modern now while supporting all the stuff that is used all over the world it inevitably becomes quite complex. As for the templates. They allow us to not pay the runtime cost for our abstractions. Which is really handy. With dynamically typed languages we pay a lot of runtime cost for their operation. Furthermore there are some context (think automotive, nuclear, military etc) that have to be fully deterministic including the memory allocation. That’s when C++ shines. It allows us to write abstract code while satisfying those requirements. And as for complexity, yes all the details are complex, but the ideas are not that complicated. In the end if you think about it, the details are complicated in any language.
@zamf4 ай бұрын
The truth is that statically-typed languages try to provide ways of declaring loosely-typed constructs, like templates, variants, type-erasure, any-type and so on, while dynamically-typed languages try to provide ways of declaring strongly-typed constructs, like for example with Typescript vs. Javascript. Each of the two approaches has its benefits and drawbacks. The main benefit of statically-typed languages in my opinion is that classes and types in general come with a lot of predetermined safety checks done by the compiler. If I declare a variable `int` I know it will hold a number inside and this is guaranteed by the language. I don't have to do any checks and I can focus on other stuff. With dynamically-typed languages you never have these compiler guarantees that a certain variable holds a certain type of value. So every time you use a variable you need to be sure that you're not working with some incompatible data. On the other hand dynamically-typed languages let you focus on the problem you're solving and not on some type-safety acrobatics you have to do like with statically-typed languages. C++ templates take the best of both worlds because you still get all the compiler checks and guarantees but you can also tell the compiler "hey, compiler, this `typename T` is a type, don't worry about it, assume it's just any type and move on". So you can still use whatever type you want. Even types that other people have defined and that you've never even thought of. As long as these types comply with your template. Edit: A good example of the type-safety guarantees of statically-typed languages is Rust's NonZero integers, like NonZeroU32. This type guarantees at compile-time (i.e. while writing the code) that the value inside will not be 0. So, if you decide to divide by this number you're 100% guaranteed that the result will be some value and not NaN. With Javascript, for example, a NaN is always a possibility when doing divisions and there's no solution rather than asking "Is this value 0? If no, then do the division. Otherwise, don't divide but return some other value instead". That is some noise in the code you don't wanna have and Rust gives you a way to avoid it.
@CodeForYourself4 ай бұрын
@@zamf a very good answer! Fully agree!👍
@bsdooby4 ай бұрын
of course <typename ...> is correct 😅
@CodeForYourself4 ай бұрын
Glad you agree 👍😉
@zamf4 ай бұрын
As usual nice and concise explanation of a complex C++ topic. Templates are possibly the language feature with the most time spent on teaching them. I don't know of any other topic that so useful and at the same time so full of corner cases, exceptions and nuances than templates and template metaprogramming. One thing that is interesting to mention is the ability to "disable" a template specialization when substitution for the template arguments is not possible, also known as SFINAE. Although, it is mostly being replaced by concepts since C++20. Maybe in a future video you could mention about concepts and how they affect the overloading rules when choosing the correct template specialization.
@CodeForYourself4 ай бұрын
Yeah. You’re totally right! I’ve been thinking what to put into this video and decided to not go too deep straight away and have a separate video that will probably combine SFINAE with concepts 🤔 But I’m still thinking about it.
@bsdooby4 ай бұрын
I recommend your channel/videos to my C++ students
@bsdooby4 ай бұрын
oh, a typo "multiple tempate parameters"...
@CodeForYourself4 ай бұрын
@@bsdooby oh-oh. In the video? Where?
@bsdooby4 ай бұрын
@@CodeForYourself Yes; one of the "blue chapter screens" @ approx. 4.45
@ytacctaccnt4 ай бұрын
Pretty good explained. Considering other videos now.. (Not robot btw :) )
@CodeForYourself4 ай бұрын
Thanks for giving it a go! Hope you like the other videos too! My favorite is on move semantics: kzfaq.info/get/bejne/odeBbJODlcWrdn0.htmlsi=wOisw4cmw4bFPg4C But people seem to love the google testing one most: kzfaq.info/get/bejne/pt56ormDy9a2gHk.htmlsi=LCAPL2nSCyROrP-X
@CodeForYourself4 ай бұрын
Hey everybody. This video took _a while_ to plan and record. Please comment if anything is unclear or, on the contrary, if you liked some explanation. Hope there won't be too many mistakes. 😅 Also, if you like what I do - smash that "Super Thanks" button 😉
@siddevsam4 ай бұрын
That clip of cat failing to jump is exact visual representation of me running my code! 🤣🤣
@CodeForYourself4 ай бұрын
Yeah, don't even mention it! Been there many times myself 🫂
@zecuse4 ай бұрын
cxx_setup is essentially working like a wrapper for our project (or rather library), right? It's sort of just separating some of the boilerplate stuff from the core of our code?
@CodeForYourself4 ай бұрын
I wouldn’t really call it a wrapper. It is a kind of library-like cmake target just with nothing to be compiled. It is a dummy target to which we can assign certain properties. Does it explain the notion a bit better?
@zecuse4 ай бұрын
@@CodeForYourself Ya. I've been playing around with my own project. I knew wrapper wasn't the best word to use for this, but everything we do with it is still inherited by the targets we link it to (because of the INTERFACE visibility). I have seen the other comment regarding the question at the end of the video regarding not including the cxx_setup in the executable's link which makes it clear that wrapper isn't the best word to use. I thought about likening it to how composition works, but couldn't think of a good noun to describe that. So, wrapper was the next thing to come up.
@erkamkocaer20974 ай бұрын
Now I'm trying to draw a 100x100 of the same cat image into the terminal but it gets too big in the terminal. How can I solve this? I cannot even draw original 360x400 in the terminal.
@CodeForYourself4 ай бұрын
Well, terminal does have a limited number of digits for us to draw in. What is the biggest image that you actually can draw? Can you draw a very small image?
@erkamkocaer20974 ай бұрын
I can draw small images likes 40x40. I did not understand how to arrange the terminal like yours to show original image perfect size on terminal. @@CodeForYourself
@erkamkocaer20974 ай бұрын
I can draw 40x40 . But I did not understand how I can draw bigger sizes like yours in the video@@CodeForYourself
@CodeForYourself4 ай бұрын
I don’t really remember which image size I print in the terminal at 0:21 but I know that I pick it by looking at how many characters fit in my terminal vertically. How many fit into yours? Reducing font size might help too if you want to see a higher “pixel” count. Does it make any sense? Or do I still misunderstand your question?
@erkamkocaer20974 ай бұрын
Okey, thanks for help. @@CodeForYourself
@Elclaapo4 ай бұрын
Very clear. I am using your lessons to complete the homneworks from the 2020 c++ course from Bonn Uni with professors Stachniss and Vizzo. Honestly without your lectures I would be lost. Huge thanks!
@CodeForYourself4 ай бұрын
Thanks for the compliment! The funny thing is that Ignacio’s course was originally derived from the one I wrote in 2018. This KZfaq series is a further iteration on that material with some more experience gained in the meantime 😅
@Elclaapo4 ай бұрын
@@CodeForYourself I know ! I have been following the “Stachniss legacy” since long time ahah! I have tried in the past learning c++ properly with your course on stachniss’ KZfaq channel, but I lost a bit of motivation since I did not have homeworks or tests. Reason why I started the 2020 course recently as they still have homework material available in their website.
@Partey-xh3fr4 ай бұрын
Маєш файну футболку
@CodeForYourself4 ай бұрын
Не кажи 😉
@ElPikacupacabra4 ай бұрын
Maybe the "helpful compiler" should not be so helpful. 😂
@CodeForYourself4 ай бұрын
Well, there are good reasons for it to be this helpful I would say 😉
@ElPikacupacabra4 ай бұрын
@@CodeForYourself Yeah, the C++ defaults are probably wrong. This is what I was getting at.
@CodeForYourself4 ай бұрын
@@ElPikacupacabra I wouldn't go as far as call them "wrong". They don't always align with the current best practices. But such best practices change. While back compatibility remains an important feature of the language. The current defaults mostly exist to enable that back compatibility if you ask me.
@Ash-fo4qs4 ай бұрын
how to do gtest and fuzztest. can you make tutorial from basic.
@CodeForYourself4 ай бұрын
There is already a video about gtest: kzfaq.info/get/bejne/pt56ormDy9a2gHk.html As for fuzzing that’s a bit more complicated in my experience and I didn’t plan to cover it explicitly just yet. But if there is a lot of interest we could do that too 👌
@Ash-fo4qs4 ай бұрын
@@CodeForYourself thanks Also if you cover fuzztest. One more request to show us how to integrate gtest and fuzztest Thanks again. Going through your Playlist, I like it.
@CodeForYourself4 ай бұрын
@@Ash-fo4qs thanks for the kind words. I won’t promise the fuzz test just yet. But I will record it as a topic of interest for after I finish this playlist.