r/cpp • u/jrruser • May 29 '20
[The Cherno] Move Semantics in C++
https://www.youtube.com/watch?v=ehMg6zvXuMY31
u/Tjccs May 29 '20
Him and Jason Turner are the ones I go for when I need to check or want to know more about C++, might not be as useful for someone with years of experience.
7
u/adammerickson May 30 '20
When you’re ready for guru level, checkout Bisqwit: https://m.youtube.com/user/Bisqwit
3
u/PhallusPenetratus Jun 02 '20
Bisqwit level is pure wizardry. Maybe someday I'll understand it.
2
20
u/ElijahQuoro May 29 '20
There are several mistakes, which are already noted in youtube comments, so if you are new and learning, check it.
9
u/adnukator May 29 '20
Outside of using
deleteinstead ofdelete[], what other mistakes are there? Not allocating the null terminator is not an issue since he's terminating the printing loop by string length and is printing chars individually.5
u/m-in May 30 '20
newanddeletebelong in library/dictionary classes and legacy code, so even mentioning those when talking about love semantics is weird. Anything new that doesn’t depend on dinosaurs (say Qt - cool toolkit, awfully outdated API), shouldn’t be allocating directly like that. If you need something special, do it in a library class, and then use that, instead of manually managing memory.11
u/adnukator May 30 '20
newanddeletebelong in library/dictionary classesLike... for example a class that holds a heap allocated string without needing the standard library, such as in the very example he's showing? If he used std::string or std::unique_ptr it would just hide his point why move semantics are useful which would sort of ruin the purpose of the video.
1
u/m-in Jun 04 '20
Crap. All those closed-source STL implementations we have to deal with…
When I’m teaching this, it’s usually with libcpp and editing it on the fly, adding instrumentation, breakpoints, etc. Why reinvent the wheel… It’s all done, and done well.
4
u/scrumplesplunge Jun 07 '20
Standard library code is pretty heavy to show to beginners. _M_naming_convention aside, it has a lot of optimisations and tricks which distract from the point for a video like this. The string header is over 4k lines of code. Writing something from scratch is much clearer for the purpose of demonstration.
1
u/m-in Jun 10 '20
I’d argue that it’s important to highlight all those bits that they have in the header: many of them are needed for a complaint implementation. Even without any optimizations it’s a big header.
1
u/scrumplesplunge Jun 10 '20
I didn't think we were talking about a compliant implementation of std::string, just a string class. std::basic_string is packed with many different features which (in my opinion) would be overwhelming for a beginner.
5
-17
u/twirky May 29 '20
Even using
deleteinstead ofdelete[]for an array of POD isn't really a mistake. Both deletes will do exactly the same thing.delete[]just calls the destructor for each element before freeing the memory.25
u/CubbiMew cppreference | finance | realtime in the past May 29 '20
undefined behavior is always a mistake
-20
u/gvargh May 29 '20
it's necessary for effective optimizations
10
u/emdeka87 May 29 '20
Pretty sure compilers will elide the call to destructor if it's trivially destructible?
16
u/Ipotrick May 29 '20
cherno is amazing
-10
u/Ipotrick May 29 '20
thou a solution like:
class Data {
...
Data(std::string str)
: m_str(std::move(str))
{}
...
}
is more elegant imo. Not 20 overloads for char ptr, string_view std::string.... . Just one Constructor for all and is only has the (very minimal) overhead of a move, but can accept all implicitly to std::string convertable types. Also very readable and maintainable with very good performance.
11
May 29 '20
I think you missed the point.
1
u/Ipotrick May 31 '20
maybe, buuuut i think something like this could be a nice addition to the video
7
u/le_manalishi_verde May 29 '20
At first when he did the cast to string&& I almost had an seizure, but then luckily he changed it to std::move
Pheew :)
2
u/ticket2020 May 29 '20
Can anyone clear my doubt please :
at first he had created the Entity object as :
Entity entity(String("Cherno"));
But at https://youtu.be/ehMg6zvXuMY?t=429 he changed it to :
Entity entity("Cherno");
And it works fine. Is there a concept I am missing here ?
Entity's constructor expects String type object, we aren't providing that. Does it happen because String class has a constructor that expects const char* ?
5
u/gauravnba May 29 '20
You are right. Since the Entity constructor is getting a const char* value, and string has a constructor that does accept a const char*, the compiler calls the constructor to string, implicitly.
I think, in another video, Cherno explains implicit casting and its drawbacks too. I can't seem to remember which one, though
3
u/ticket2020 May 29 '20
Got it. Thanks.
So, if we can create object like :
String ob = "Cherno";
This calls the copy constructor since there is a new object being created and we have defined an appropriate copy ctor.
Similarly when
Entity entity("Cherno");
is called, it is implicitly converted to String.1
1
0
u/vickoza May 30 '20
There is a mistake in the video the delete in the destructor should be delete[] this was mentioned by me add at least 2 other.
0
u/ostkaka0 Jun 04 '20
Not a fan of move-semantics. None of this is needed if you manually free memory rather following RAII. Code becomes more readable and there is less paperwork to do(copy-constructors and move-constructors).
58
u/rekosin1 May 29 '20
This guy has taught me more than my professors did....Thank you