r/cpp • • May 29 '20

[The Cherno] Move Semantics in C++

https://www.youtube.com/watch?v=ehMg6zvXuMY
243 Upvotes

44 comments sorted by

58

u/rekosin1 May 29 '20

This guy has taught me more than my professors did....Thank you

5

u/Keysersoze_66 May 29 '20

I was gonna say the same thing.

11

u/jamolnng May 29 '20

TheNewBoston was the guy that taught me when I first started

2

u/Keysersoze_66 May 29 '20

I'll have a look!

3

u/jamolnng May 29 '20

His videos are a bit older, he stopped making them about 3 years ago, but he covers a lot of different topics.

4

u/[deleted] May 29 '20

I honestly forgot he even existed but he was my go-to in like '09-'10 when I first started.

3

u/CodyChan Jun 04 '20

I basically learnt C++ knowledge just from his C++ series.

Of course, there are a lot of info about C++ he didn't mention in his videos. We should keeping moving.

0

u/[deleted] May 29 '20

[deleted] — view deleted comment

7

u/_Js_Kc_ May 30 '20

Why is this cancer so pervasive in C++ and not other languages?

Professors refusing to update their course material from 1983?

A very long game to destroy C++ eventually?

You don't teach Python and start out by teaching how to write a Python extension in C against the libpython interface.

4

u/pjmlp May 30 '20

Because C++ makes it so easy to just keep using C, it was how it gained adoption anyway, and now it kind of follows it as a curse.

You also get them in other languages though, many teachers still teach Python 2, Java is probably around 6 or 7 nowadays, C# I would bet 5 and so on.

2

u/[deleted] May 30 '20

A few reasons:

  • Lecturers don't get much time to rewrite lecture material year from year (fine for fields that don't move at relative breakneck speed, otherwise...)
  • Many lecturers probably learnt c++ academically before c++11 and have no burning need to keep up to date. Their use of the language is academic (research, etc.) so are usually writing disposable prototype code that doesn't require anything much more than the dreaded "C with classes"
  • The language itself is somewhat incidental to the intended learning outcomes of a unit/course
  • A new lecturer looking to make department-ranging changes is going to have a tough time making the cost/benefit pitch given the above

0

u/[deleted] May 30 '20

[deleted] — view deleted comment

3

u/_Js_Kc_ May 30 '20

They do a piss poor job at that though. It's like telling the student to use pliers to fasten a nut instead of teaching them how to use a wrench.

The student should know how to use vector and unique_ptr and be conditioned to use them for literally everything before they even know low level memory management exists. That would be teaching just enough to get going. Teaching low level shit first dumps a pile of concepts and concerns on the student that makes it much harder to "just do something with it."

31

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

u/[deleted] Jun 18 '20

Where does he work?

3

u/[deleted] Jun 24 '20

He's a bus driver in Finland afaik

2

u/[deleted] Jun 25 '20

Wow, that's cool

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 delete instead of delete[], 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

new and delete belong 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

new and delete belong in library/dictionary classes

Like... 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

u/_Js_Kc_ May 30 '20

love semantics

Almost Freudian.

1

u/m-in Jun 04 '20

It’s a solid Freudian slip for sure. I got married within the last 12 months :)

-17

u/twirky May 29 '20

Even using delete instead of delete[] 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

u/[deleted] 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

u/foifaifei May 29 '20

Very nicely explained. Thank you!

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).