In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is the analysis of the principle of the special function generation mechanism of the class in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Rules for generating Special functions of C++ Class
Refer to the instructions on Effective Morder C++:
Default constructor: generated only if the class does not contain a user-declared constructor.
Destructor: generated by default, when the destructor of the base class is virtual, the default destructor of the derived class is the virtual function.
Copy constructor: generated only if the class does not contain a user-declared copy constructor. If the class declares a move operation, the copy constructor is defined as deleted.
Copy assignment operator: generated only if the class does not contain a user-declared copy assignment operator. If the class declares a move operation, the copy assignment operator is defined as deleted.
Move constructor and move assignment operator: generated only if the class does not contain user-declared copy operations, move operations, and destructors.
Example: a BUG
Because you are not familiar with the generation mechanism of destructors, this leads to a BUG.
First of all, there is no problem with the following code, because the Widget is also a shift-only type by default because of the data member Widget; it is also possible to insert a std::pair constructed from a shift-only type in mm, because pair by default supports right-value parameter construction (which can be constructed by move-only Widget) and its own mobile constructor (which can be moved to unordered_map):
Class Widget {public: Widget () = default;// ~ Widget () = default;private: std::thread type; / / Type only}; unordered_map mm;mm.insert ({12, Widget ()})
Then, I added a default destructor:
Class Widget {public: Widget () = default; ~ Widget () = default;private: std::thread masks; / / Type only}; unordered_map mm;mm.insert ({12, Widget ()}); / / error!
The error message is extremely long, and the core errors are:
Error: no matching function for call to 'std::unordered_map::insert ()' 45 | unordered_map mm
The construction of the std::pair can be extracted separately to see a clearer error message:
/ / the code is as follows: make_pair (12, Widget ()); / / the error report is as follows: In template: no matching constructor for initialization of'_ pair_type' (aka 'pair')
Obviously, because the mobile constructor of Widget is implicitly deleted (it can neither be copied nor moved), a std::pair cannot be constructed from the Widget parameter.
The solution is not to define a destructor, or to explicitly define a mobile constructor:
Class Widget {public: Widget () = default; Widget (Widget&&) = default; ~ Widget () = default;private: std::thread masks; / / Type only}; unordered_map mm;mm.insert ({12, Widget ()}); examples: std::mutex and std::thread
When I was doing the experiment, I mistakenly remembered std::mutex as a shift-only type at first and defined a class like this:
Class Widget {public: Widget () = default;private: std::mutex masking;}; unordered_map mm;mm.insert ({12, Widget ()}); / / error!
Widget can't be copied and moved even when I haven't added destructors.
Look at the source code:
Class mutex: private _ mutex_base {public: / *... * / mutex () noexcept = default; ~ mutex () = default; mutex (const mutex&) = delete; mutex& operator= (const mutex&) = delete; / *. * /}
Obviously, because mutex defines its own default destructor and defines the copy constructor as deleted, its move constructor is also implicitly deleted, so mutex can neither copy nor move.
Compare with the std::thread source code:
Class thread {public: thread () noexcept = default; thread (const thread&) = delete; thread (thread&& _ t) noexcept {swap (_ t);} ~ thread () {if (joinable () std::terminate ();}
Although std::thread defines destructors and deleted copy constructors, it explicitly defines the move constructor, which makes it impossible to copy but moveable.
Thank you for reading! This is the end of the article on "Analysis of the principle of Special function Generation Mechanism of classes in C++". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.