In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how C++ uses naming conversion". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
ES.49: if type conversion is necessary, use naming conversion
Reason (reason)
Readability. Avoid mistakes. Naming conversions are more explicit than C-style conversions or functional transformations, allowing the compiler to catch more errors.
The named casts are:
Naming transformations include:
Static_cast
Static conversion
Const_cast
Constant conversion
Reinterpret_cast
Reinterpret the transformation
Dynamic_cast
Dynamic conversion
Std::move / / move (x) is an rvalue reference to x
Mobile / / move (x) is the right reference of x
Std::forward / / forward (x) is an rvalue or an lvalue reference to x depending on T
Value transfer / / depending on the type of T, forward (x) is a left or right value reference
Gsl::narrow_cast / / narrow_cast (x) is static_cast (x)
Narrowing conversion / / narrow_cast (x) is static_cast (x)
Gsl::narrow / / narrow (x) is static_cast (x) if static_cast (x) = = x or it throws narrowing_error
Narrow conversion (throw an exception) / / if static_cast (x) = = x recording (x) is static_cast (x), otherwise an exception is thrown
Example (sample)
Class B {/ *... * /}
Class D {/ *... * /}
Template D* upcast (B* pb)
{
D* pd0 = pb; / / error: no implicit conversion from B* to D*
D* pd1 = (D*) pb; / / legal, but what is done?
D* pd2 = static_cast (pb); / / error: D is not derived from B
D* pd3 = reinterpret_cast (pb); / / OK: on your head be it!
D* pd4 = dynamic_cast (pb); / / OK: return nullptr
/ /...
}
The example is a collection of errors collected from the actual code on the premise that D used to inherit from B, but someone refactored the inheritance relationship. The danger of C style conversion comes from the fact that it can be any type of conversion, which obliterates any possibility of error protection (now or in the future).
Note (Note)
If you want lossless conversions between types (for example, from float to double, or from int32 to int64), consider using curly braces initialization instead.
Double d {some_float}
Int64_t i {some_int32}
On the one hand, this method defines the intention of type conversion, on the other hand, it can prevent the loss of precision during conversion. (for example, in the case shown in the code, a compilation error occurs if the float variable is initialized with the declare value)
Note (Note)
Reinterpret_cast can be essential, but the essential uses (e.g.turning a machine address into pointer) are not type safe:
Reinterpret_cast is essential, but this necessary use (for example, converting machine addresses to pointers) is not type-safe.
Auto p = reinterpret_cast (0x800); / / inherently dangerousEnforcement (implementation recommendation)
Flag C-style and functional casts.
Remind of the conversion of C style and function form
The type profile bans reinterpret_cast.
Type rule group forbids reinterpret_cast.
The type profile warns when using static_cast between arithmetic types.
The type rules group warns against the use of static_cast when converting between arithmetic types.
Translator's note:
C style conversion: B = int (a)
Function form conversion: b=int (a)
That's all for the content of "how C++ uses naming conversion". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.