I really wish I had've just watched your entire C++ series instead of listening in any of my lectures. This has been miles more helpful and easy to understand than any programming class I've had.
@Dante30854 жыл бұрын
A good ways to understand std::move() is that you are basically saying "I don't need the object that I am passing anymore. I am allowing someone to steal from it."
@supremedeity90034 жыл бұрын
Voluntarily letting your stuff be stolen.
@glee210124 жыл бұрын
It basically steals the pointers, or takes ownership of them, very powerful, like Cherno says "no new allocation". I can only think of using it when I have a temp variable.
@Evan490BC3 жыл бұрын
@@glee21012 The fundamental Computer Science concept here has to do with Type Theory, and in particular linear (more correctly, affine) types: any object can only be owned by a single owner at any given time. Because C++ is the way it is (i.e. a mess) it's harder to enforce. But it (i.e. C++) is a necessary evil.
@anon1963 Жыл бұрын
@@Evan490BC c++ is only a mess when your bad code makes it seem like a mess.
@Drastonar3 жыл бұрын
This is something I did so many times in C, but my teachers never told me it was a concept (move). It's much clearer when to do what now and the class abstraction we can do in C++ helps the code to be cleaner.
@maysanghai1232 жыл бұрын
I think what you must have been doing is a simple typecasting in school. This is just a specific version of typecasting which works specifically with rvalue references.
@fabricedelannay72883 жыл бұрын
14:05 distinction between constructor and assignement operator, that's why I rather prefer to use {} brackets with new object creation and use the = only for assignement operator :) (For instance : String name{"Cherno"};) By the way, huge and awesome work, thanks !
@brandoncfrey4 жыл бұрын
Not a clue what most of what you said means on a technical level. But still couldn't stop watching. Great video. C++ gods help me 😭
@ameynaik27432 жыл бұрын
1. Instead of casting using (T &&)var you can use std::move(var) 2. A good ways to understand std::move() is that you are basically saying "I don't need the object that I am passing anymore. I am allowing someone to steal from it." 3. We need to always delete the current data before shallow copying the other data. Also we need to point other data to nullptr.
@malborboss6 ай бұрын
4. Before moving the other object into ours we need to ensure that we are not moving the same object to itself. Otherwise data may be lost.
@justinjames30282 жыл бұрын
I did not understand std::move() until I watched this. To give some background, I was first exposed to C++ in 1994 but moved away from it before r-value references were a thing. I like the examples he provides. The problem I have with most explanations is that they use words like "tells the compiler it can cannibalize the object" or some such nonsense. That leaves the impression that the compiler is doing some type of cannibalizing or other magic behind the scenes. Saying it just turns the object to an r-value reference and as a side effect can cause the move constructor or move assignment operator to be called. The person cannibalizing is not the compiler, it is the implementor of those operations. After seeing this I wrote a simple class like he had and played around with it. It is not nearly as complicated as I thought.
@boondocksripoff22374 жыл бұрын
I now realize I still dont know the basics of c++
@1vader4 жыл бұрын
I thought the same thing after watching the video but honestly, it more so made me realize what a crazy and confusing language C++ is. Stuff like this is why I prefer C or Rust.
@jyl90634 жыл бұрын
Move constructors/assignment is basic C++
@PixelThorn4 жыл бұрын
@@1vader or C#, or Python...
@Dante30854 жыл бұрын
@@1vader That's not even the difficult part of the story of move semantics. Research perfect forwarding and reference collapsing
@Stefan-tw3fo4 жыл бұрын
@@1vader Same here.
@jonathanp51952 жыл бұрын
Thank you for the video. I think what "clicked" hardest for me was when I learned that the assignment operator, by default, only moves the values inside an object to a different object, if the object that it is being moved into pre-existed - which is why we overload the assignment operator, so that we can do this, even when the object that the data is being moved into is being initialized. Or at least, this is how I think it works. If I'm misunderstanding, I would love some clarification.
@astronautid79482 жыл бұрын
Good point! In a way the moving is the best thing that can happen to m_Data inside that operator. And we're talking the move assignment operator and the moving as opposed to copying which is what memcpy does in the copy constructor.This line says it all: m_Data = other.m_Data; Don't get disillusioned, however, thinking the data is being copied here. It's only a pointer being copied to another pointer. Imagine m_Data (in other) points to a char array the size of like billion. The array needn't to be copied, it stays where it was in memory. That's when we like assignment by move better than by copy. And we for this reason overload the move assignment operator. You got the gist.
@mikeweathers57264 жыл бұрын
Great video! This is my favorite C++ series. I hope you make one on perfect fowarding soon.
@johnnyserup55002 жыл бұрын
you definitely made a cool explanation of the std::move functionality - thanks, this is going to help many developers I think
@lucascoef Жыл бұрын
I've just aced a C++ technical interview thanks to this video. Your ability to explain is unparalleled. Keep doing the good work!
@svenbtb Жыл бұрын
i know this is 7 months old lol but congrats! Hope the job is going well and I hope to one day soon ace a C++ interview myself using what i learn from Cherno
@varuntaneja70734 жыл бұрын
I don't earn right now so can't really support you on patreon but the least I can do is watch all the ads on your videos :)
@kartikgarasia56853 жыл бұрын
Man now I just feel bad.. I also should pause adblocker on this channel
@Rohith_E4 жыл бұрын
Your examples make move semantics really easy to understand. Please explain copy and swap idiom too.
@thestarinthesky_4 жыл бұрын
The more I learn about C++, the more I realize that I know NOTHING about this complicated but amazing language! I could learn C++ for the rest of my life and still there would be much to know! 🤩😍❤️🥳
@__jan4 жыл бұрын
that's not a good thing
@thestarinthesky_4 жыл бұрын
@Prochy it is just the matter of preferences☺️ I would love to be challenged and C++ is a REAL programming language! C++ helps me to realize what is happening behind the hood, C++ gives insight into things I can’t see in other language! C++ is a miracle in the world of computer science! C++ being huge and big, makes you keep learning and learning! There will be always something new and challenging for you! That’s what I want! Never stop learning! 🙏❤️
@oracleoftroy4 жыл бұрын
@@__jan Some people like dead languages that never change, others like new features that improve their code clarity and performance. I'm of the opinion that C++ being an actively improved language is a very good thing, and having to continually learn new things is part of the job description of any software developer.
@spicy_wizard4 жыл бұрын
perfect forwarding ~
@deepikagoyal07 Жыл бұрын
Move semantics is a topic difficult to comprehend. Your in depth explanation made it very clear.
@z0lol4 жыл бұрын
great video! well explained. would be great having a video regarding Variadic functions :D
@alecmather Жыл бұрын
Please never stop making videos.
@ianpan01024 жыл бұрын
Incredibly helpful and enjoyable video -- thousand thanks Cherno!
@SirShrimpey4 жыл бұрын
Dope, some time ago I was looking for a nice explanation of move assignment operator and exisiting stuff was quite confusing, I could have used a tutorial like this.
@roboticsrecords4 жыл бұрын
I believe explanation in chapter 5 in "Effective Modern C++" by Scott Meyers is one of the best source I've read on this topic. I have a paper copy, but it seems you can find it for free on the internet.
@TheDada0106 Жыл бұрын
You're a legend, learning c++ with you is exciting !
@igniculus_4 жыл бұрын
I can't believe he is still making C++ videos. This just shows how much there is to know about C++ and how little I know ... :P
@lordmushroom7234 жыл бұрын
it's because this language has become extremely bloated and a pain in the ass to work with
@alpyre4 жыл бұрын
@@lordmushroom723 Words of wisdom. C++ has too many features to solve the issues that are there because of C++ itself.
@PflanzenChirurg4 жыл бұрын
@@alpyre not really, it has just its rules and every new standard makes it easyer to create decent code
@RWM_3 жыл бұрын
@@PflanzenChirurg Yes, like here when you must create specific constructors and operators :)
@SwissPhil02 Жыл бұрын
this is really well explained! Wish I would have watched your series before my interview...
@kelvinsmith48944 жыл бұрын
Awesome work you’re doing here!!, do you plan on making videos on error handlings and file system read and write in C++?
@Raspredval13374 жыл бұрын
file read and write is rather simple, use , open a file and use it as cout or cin
@farhan7874 жыл бұрын
Cherno finally a new video for C++ series, thanks ♥️
@Mohammed244414 жыл бұрын
A video on lifetime extension, return value optimization, forwarding...
@buzzdx4 жыл бұрын
very good explanation, thanks. i used to struggle understanding move semantics. one question though: in the string destructor you used "delete m_data;" while in the move operator you used "delete[] m_data". since you allocated the memory using new[], i think the destructor should use delete [] too. however it's not crashing. does it make a difference really which one you use to delete?
@oracleoftroy4 жыл бұрын
It is undefined behavior. As you point out, he should be using operator delete[].
@SerBallister4 жыл бұрын
In the case of POD types there are no destructors so new and new[] behave the same in this case. For none POD classes/destructable classes the compiler/implementation does do some trickery like encode the number of objects belonging to the array so it can later call the destructors on them all. It is best practice to delete[] though as some compilers/implementations may not simplify array allocations (most compilers do, but that's not a guarantee!)
@oracleoftroy4 жыл бұрын
@@SerBallister It is undefined behavior and thus is illegal C++ and shouldn't be relied on. It might work on your machine, but not other machines, and not your machine tomorrow as compiler and standard library implementer figure out better ways to optimize your program. A simple example, an allocator might pool all allocations for a single byte into a pool of memory such that a call to `delete charptr;` would be assumed to come from this pool whereas `delete [] charptr;` would expect the object to be allocated in a different pool (perhaps a large object pool). As the implementer knows it is illegal to use `operator delete` for memory allocated with `operator new[]` they can avoid storing extra information that isn't needed for a legal program and are under no obligation to properly cleanup programs relying on undefined behavior. Being a primitive type or not having a constructor isn't a reason to accept undefined behavior in a program.
@joebosah27273 жыл бұрын
Shall get there and will overcome Thank you, Cherno
@paulchoudhury2573 Жыл бұрын
Excellent presentation. Well organized and full of important details.
@apelsin3001 Жыл бұрын
Thank you much! very understandable!
@simonmaracine47213 жыл бұрын
Now I realize that Rust is basically doing move semantics by default. And to do a deep copy, you have to clone() the variable. let mut dest = String::new(); let simon = String::from("Simon"); let other = String::from("other"); dest = simon; // This is a move; simon is now gone println!("{}", simon); // Compilation error dest = other.clone(); // Both dest and other are available println!("{}", other); println!("{}", dest); // Works In C++ is the other way around. By default deep copying is used.
@charlesmola4 жыл бұрын
Maybe you can do a video about Hash Tables!! it would be very useful. Thanks for the videos btw, they help me a lot
@jackofnotrades154 жыл бұрын
Could you do a video on streams? iostream and fstream class heirarchy and some stream handling functions?
@Dante30854 жыл бұрын
i literally have been waiting and researching this the last few days. nice !
@reza-nr2es4 жыл бұрын
wow the way i never knew that std::move is just a small template function which just simply does the casting thing for us...and all that casting does is calls the move constructor...the lazy and dumb me never tried to look up the definition of that std::move function....now i know...thanks cherno!!! but actually have a question...in order to use the move constructor, do we always explicitly have to tell the compiler by casting the type into a rvalue ref??? or there are cases where the compiler does it implicitly??? thanks in advance!!!
@AbhishekKumar-el3ls9 ай бұрын
While overloading the move assignment operator, should we not allocate memory for m_Data? If the destination string length is greater than the original string we are moving into, there would be a memory access violation. String orig = “Hello”; String dest = “LongerString”; orig = std::move(dest); can somebody explain?
@poganka45 Жыл бұрын
For me the code crashes in debug mode. On line 96 dest is constructed with default constructor, but it doesnt initialize the private members on the lines 60 (char* m_Data) and 61 (uint32_t m_Size), so when dest.Print() is called on line 101, the for loop on line 54 actually starts - because unitialized m_Size is a garbage value, and m_Data[i] (which is a nullpointer) causes the crash. So i think char* m_Data should be set to nullptr uint32_t m_Size to 0.
@afterlife1578 Жыл бұрын
any idea why it worked for him? crashed on mine too
@JATmatic4 жыл бұрын
Rule of five.. rule of five! Please, explain this and do the string class with copy-ctor && swap() idiom! :)
@jediflamaster9 ай бұрын
Just swap the values in move assignment instead of setting to default. You can skip the whole "what if they're the same object" problem
@TheMR-7774 жыл бұрын
Wow! Really Nice! Many Thanks 4 this concept!
@luyuanqi29603 жыл бұрын
Dude you are a live saviour
@marioterresdiaz60012 жыл бұрын
Great video! Got a question though, is it possible/recommended avoiding copying when the object is returned?
@adityanandagudi2 жыл бұрын
awesome!! Thanks buddy
@lucagagliano51184 жыл бұрын
I understand the basic idea. But I can't really figure a realistic case where I need to implement these explicitly. Usually you want to avoid heap allocation and people use a lot the stl library. Unless you're writing your own library I can't really picture a situation where you need to implement move constructors and move assignment explicitly. You also have default move constructors, but I don't know how they works.
@UsernameUsername0000 Жыл бұрын
2 years late. You’re right that the STL encapsulates functionality. In cases where your class is composed of STL containers and/or smart pointers, then the default move constructor generated by the compiler would suffice. It does a member-wise move of all the members. If you use modern C++ and are more of a consumer than a creator, then you would likely never have to explicitly define those functions. Still, it’s good to be aware of the specifics of move semantics to allow you to leverage them well.
@maniaharshil9 ай бұрын
Please cover the forwarding and arguments decay too Thanks
@niekbeijloos83552 жыл бұрын
Great Videos you make! Keep it up!
@xxdeadmonkxx4 жыл бұрын
No one: Voice recorder: blinking green as Cherno say something
@myronkipa2530 Жыл бұрын
Cpp devs learning basic rust concepts
@carnaqe11549 ай бұрын
how can you Print the uninitialized m_data member without an error?
@aleksandarfranc10943 жыл бұрын
You are a legend man!!
@tiantianliu59589 ай бұрын
Great! keep going!
@JaiHall4 жыл бұрын
Very insightful, thank you. 🙏🏿
@Upload29713 жыл бұрын
Why don't you share these example codes so that we can execute and experiment easily ? Thanks
@vrushaliterekar40304 жыл бұрын
I have learnt basic components of C++ as a beginner and have tried to search 1000 ways of learning C++ as I am a self-taught person. The next step many people suggest is to solve the problems but I have solved basic problems such as calculater and some basic problems. Please could someone could help me with what projects should I consider which are not too advanced. Also some examples would do. (I am reading Effective C++ as Cherno suggested in one of his videos) I'd be so so glad if someone could help me as mentor
@VoidloniXaarii Жыл бұрын
Thank you very much
@steilcacciatore71942 жыл бұрын
confused here.. so you can use move method as a swap method to swap two variables? like std::move twice between the two variables, will it work?
@hervelebars2 жыл бұрын
No because std::move does not move anything. It is just casting a variable to a rvalue reference (type&&). Casting is not moving. Ok, the name is misleading, but that is it.
@sumedhkulkarni53462 жыл бұрын
Hi, Awesome explanation, 😀 one question, why not pass objects by reference if want to give the ownership of the object to the function? I know that move will invalidate the first object and literally move it in the function space. but in a case, where we just want to avoid copy while passing to the function and the application is single-threaded even the pass by reference should work right?
@vaibhavlohiya83962 жыл бұрын
That 's what he is doing isn't it. He is using the rvalue reference i.e (String&&) to move the object, similar to what he did in the Entity class in the previous video. And instead explicitly casting to the object, he is using the std::move. Furthermore, just passing it by reference will only be valid for lvalues, now the work around for that is const reference but for that also you have to copy the data. I hope this helps.
@sergei83373 жыл бұрын
what's the benefit over usage of pointer/refs here? In other words why move if we can just refer to the data?
@ericjovenitti6747 Жыл бұрын
So one you’re an absolute amazing teacher and thank you for doing us, too maybe give use case examples for this I still factor five years don’t completely understand the best used cases for this. Is this more conducive for embedded systems or extremely low latency code?
@ryanmcgowan30614 жыл бұрын
Cherno, I'd love to see a video on your thoughts on Rust.
@roboticsrecords4 жыл бұрын
Also probably D language. D lang has already been used by gaming industry in "Quantum Break". I am not aware of any uses of Rust in games, but would be curios to learn what gaming industry thinks about it.
@matteopossamai11034 жыл бұрын
@@roboticsrecords Firefox is written in rust. I know it's not a game but it's more than enough of a reason to talk about it, and also since a browser is such a resource intensive task it probably makes sense to build a game engine with it
@dieSpinnt4 жыл бұрын
@@matteopossamai1103 Just a side-note: Take a step back and look what you are using now. This massive resource hungry monster, polluted with backwards compatibility and security nightmares like javascript isn't really a clever starting point for a game(engine). I second @Vlad Tananaev's suggestion because D(dlang) is wonderful. But you need experience to show something. I don't know if we should annoy Yan with it. At all, a little "gfm:opengl" and a pinch of "dlangui" (these are D packages) -> ready is your game:)
@matteopossamai11034 жыл бұрын
@@dieSpinnt This channel isn't just about game engines. He has a C++ series with something like 90 videos iirc and rust is a language that actually tries to fill the same spot as c++, whereas D has a garbage collector. I don't even know what in my comment made you think this was about Firefox, it isn't. The point is that it is a language that is used in the industry in a way that is similar to c++. (Favourite language of stack overflow for the last 5 years, and maybe there is a reason for that). About dlang... this is just my ignorant opinion as someone who never used it, but since not a lot of people use it wouldn't it make sense to use another compiled language with garbage collector such as Go? What about D is so revolutionary?
@obinator90654 жыл бұрын
matteo possamai Not so many parts of Firefox are written in Rust (The CSS Engine) but many are still implemented in C++, they are rewriting some more parts but that is a ginormous project and it’s gonna take some time because these code bases are big and complex. Microsoft is also looking into Rust right now as 70% of security issues usually are memory leaks. As far as gaming goes there’s the piston engine and amethyst. These are pretty usable in the current state.
@wizhippo4 ай бұрын
Should the deconstructor not use delete[] just like the operator as as new[] is used?
@whythosenames4 жыл бұрын
1:48 the classes are bytesized... that was a little nerdy but yeah i liked it :)
@vitordeoliveira61392 жыл бұрын
FOR LINUX USERS OR PEOPLE WHO IS HAVING ERRORS IN THE CODE: JUST DO THAT IN THE BEGINING class String { private: /* data */ char *m_Data = nullptr; uint32_t m_Size = 0; AND THAT ON THE PRINT void Print() { if (m_Size != 0) { for (uint32_t i = 0; i < m_Size; i++) { printf("%c", m_Data[i]); } } printf(" "); }
@ankitbhandari52222 жыл бұрын
can you explain why my default constructor cannot provide value to m_data and in terminal showing free(): invalid size error when I try to delete m_data
@vitordeoliveira61392 жыл бұрын
@@ankitbhandari5222 How is your code: is this way? private: /* data */ char *m_Data = nullptr; uint32_t m_Size = 0; public: String() = default; String(const char *string) { printf("Created "); m_Size = strlen(string); m_Data = new char[m_Size]; memcpy(m_Data, string, m_Size); }
@judickaelsam31035 ай бұрын
Thank you so much. Can you do it for std:: forward please?
@jorsch46892 жыл бұрын
I ran into a free() error while following along. Just in case someone else also gets something similar, here's how I fixed it. Error: free() invalid memory Compiler: gcc OS: Pop OS (Linux) Solution: Replace `String() = default` with a properly initialized default constructor. ``` String() : m_Data {}, m_Size {} { } ``` This problem might happen intermittently because it is entirely dependent upon how the memory at the location assigned is configured at the time.
@etinosaizekor65332 жыл бұрын
Awesome!
@ankithmanjunath30524 жыл бұрын
HI Thank you for the amazing video. At 10:17 , you mention that "If they are different objects, then nothing needs to be done !", this should be rather "If they are the same objects, then nothing needs to done", am i correct ?
@Gurman8r4 жыл бұрын
another option is implementing move constructor/assignment in terms of a member swap function.
@Raspredval13374 жыл бұрын
a man of culture as well ツ
@oraz. Жыл бұрын
Using the initializer String string = "hello" with implicit conversion is a confusing addition.
@igorszemela16102 жыл бұрын
great vid
@nexovec4 жыл бұрын
That weird feeling when you move into yourself and refuse to do anything
@HolyBucketsYT3 жыл бұрын
This begs the question of why should you EVER go back to using copy constructor when an object requires allocated data if deleting and reallocating data would always be burdensome? General rule, if your class makes use of "new" use move constructor, else just use copy constructor?
@HolyBucketsYT3 жыл бұрын
@Peterolen that would be an argument in favor of my point. Move operation preserve the object - copy construction must rebuild a new object. So I ask why ever copy construct?
@HolyBucketsYT3 жыл бұрын
@Peterolen ah, yes, I feel quite silly now... If you actually want to make a "copy" then the copy constructor is quite useful. Duh. Thanks!
@nortski784 жыл бұрын
KZbin getting ridiculous with the ads now, I mean 4? wft
@_slier4 жыл бұрын
Hi Yan..what your thought on Rust..alot of people love Rust but honestly, the only things i like about Rust is its package manager ( Cargo )..other than that, i dont see any benefit from using it...its syntax is maddness...
@nagarjunvinukonda1623 жыл бұрын
Can you make a video on push_back vs emplace_back used in move semantics when calling constructors.
@q1chen2 жыл бұрын
copy constrcutor and copy semantics plz
@saiprasadchindam51854 жыл бұрын
Please do a video on memory allocators
@austinfritzke93054 жыл бұрын
That was a painfully forced product promotion.
@billywap2 жыл бұрын
13:40 isn’t it the rule of zero?
@ocrap73 жыл бұрын
Why is STL code so messy? Do people actually program like that?
@jasangm45524 жыл бұрын
When I write delete[] m_Data; Am I not eliminating the pointer? How is it possible I can assign data after that at m_Data = other.m_Data? If the pointer still exists... does that mean that when I call a destructor to delete any pointer, like: ~Obj(){delete[] ptr;} this pointer (ptr) still exist? Is it not a memory leak? How is it different from the operation above?
@kaa_pex Жыл бұрын
looks like rust lang from the box.
@Didi-si5cs4 жыл бұрын
Hi Cherno, would you please make some videos about c++ windows form / desktop application.
@manuco2012 Жыл бұрын
What if we use std::move on an object that does not have heap allocated memory (for example a class that only has ints as members)? In this case the object won’t be moved but copied right? Is there a use case for using std::move like that (or even with pod types) or it only makes sense for objects with heap allocated memory? PS: Great video, love the c++ series!! ❤
@nextProgram Жыл бұрын
I believe all the examples he gave here were already allocating on the stack. He didn't use new() so there shouldn't be any heap allocation here.
@apivovarov2 Жыл бұрын
@4:07 in his example he used char* m_data = new char[size]
@dash8497 Жыл бұрын
we dont need to move objects allocated on the heap we can just pass the pointer :) The whole point of moving is to transfer ownership of an object. But no instance owns heap allocated objects. They only posess pointers to that object.
@apivovarov2 Жыл бұрын
std move is just a cast which tells the compiler to use move ctor if possible . Nova days classes can have two constructors - Copy ctor and Move ctor. So , new object will be copied (constructed) anyway. std Move (cast) just helps compiler to select move ctor which supposed to do shallow (cheap) copy
@Shubhsingh2562 жыл бұрын
where can I find code
@glennstormdesign4 жыл бұрын
SuperawesomethankyouYan!
@pranjalverma91714 жыл бұрын
How many topics are left in this series?
@scoutthespirit11334 жыл бұрын
have you heard of Jai programming language,? id be willing to help out any way i could to see your professional thoughts on a new language like that, it cuts out alot of standard things from c++
@Gloryisfood3 жыл бұрын
Maybe it's easier to understand by saying "std::move() turns a variable/object to an rvalue"?
@fuzzyrock31464 жыл бұрын
You are the best teacher on CPP topics ;)
@IllumTheMessage4 жыл бұрын
Muchos gracias
@travel_is_fun34554 жыл бұрын
you are great
@alfonsld76844 жыл бұрын
What "noexcept" means or does?
@treyquattro4 жыл бұрын
standards committee made a bit of a booboo: probably should have called std::move() std::make_moveable() or somesuch since move() implies a function that actually moves something. All it does as you showed is ensure whatever is being moved is an rvalue reference.
@Raspredval13374 жыл бұрын
imagine ur code, covered in std::make_movable(strName), std::make_movable(strAdress)...
@treyquattro4 жыл бұрын
@@Raspredval1337 it's more meaningful than std::move() is it not? std::move() doesn't move anything; make_moveable makes something moveable. Self-documenting code.
@oracleoftroy4 жыл бұрын
@@treyquattro I dislike that. It expresses the intent to move the object and how exactly it does that (or whether it did anything at all) is an implementation detail. Code that tells you exactly what it does rather than the intent is typically poorly abstracted. E.g. I'd rather call std::sort() than std::timsort_except_on_mac_it_is_quicksort_unless_there_are_less_than_100_items_or_75_on_android_then_it_uses_insertionsort(). I don't care how it happens most of the time, I just want to communicate the intent to sort or move or whatever other operation is needed.
@treyquattro4 жыл бұрын
@@oracleoftroy I would say that's a bad example. sort() actually sorted something (unless it was already sorted). move() simply ensures the reference is rvalue for the real subsequent move operation which might not even happen. I would expect your convoluted timsort example to do what it says on the tin otherwise you're making programmers go and read the code to find out what it actually does. Code, particularly library code, should be straightforward, unambiguous and not have side-effects. STL is replete with identifiers - most under the covers - that are more descriptive. If the standards committee had maintained any consistency, move() would have moved something like copy() copies something. It's just a poor naming choice and confuses pretty much everyone who has to get to grips with move semantics.
@platinoob__24954 жыл бұрын
do build-in types (int, float, etc) have their own assignment operators and constructors?
@aldrinaldrin46184 жыл бұрын
int, floats are called primitive types which have fixed sizes in memory. Constructors and Destructors and operator overloading are features of user-defined types like structs and classes.
@НиколайЗаднепровский4 жыл бұрын
nice video
@BobChess7 ай бұрын
I thought C++ would just be a little harder than C. I am wrong
@SirWrexes2 ай бұрын
One month into self learning it now, after a couple years doing C and I couldn't agree more lol.
@Didi-si5cs4 жыл бұрын
Australia is entering winter.
@antutucat82314 жыл бұрын
what is the difference between "nullptr" and "NULL"
@Arganoid4 жыл бұрын
NULL is #defined as 0, whereas nullptr has pointer type. So you couldn't say "int x = nullptr" because nullptr is not of type int.