In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the difference between const and constexpr in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between const and constexpr in C++".
I. comparison between Const constant and # define
Define is a simple replacement, there is no type, const can be tamper-proof and type-safe.
And # define may have multiple copies in memory (several replacements). It is the same for literal constants with or without const, for example, const char* arr = "123";, stored in the constant area, there is only one copy. For local objects, constants are stored in the stack area, for example: void add () {const char crr [] = "123";}, where "123" should be stored on the stack, but the compiler may do some optimization to put it in the constant area; for global objects, constants are stored in the global / static storage area; using const will use less space and be more efficient than # define.
Here is a small example: char* brr = "123"; char drr [] =" 123"; the former string 123exists in the constant area and cannot be modified through brr; the latter "123" is stored in the stack area and can be modified through drr.
Now, except for some specific uses of C++, it is recommended to replace macros with const,inline,enum and other macros-- from "Effective C++" clause 02
2. Const modification 1. Modifies ordinary variables, which must be initialized
Const int a = 10; represents the int object a, which is a constant and cannot be changed. From the point of view of the binary generated by the compiler, the generated an is stored in the .rodata section, that is, the readonly area. However, it is not absolute. In some cases, the optimization level is high, and the address is not taken, which may be optimized to immediately count in the .text section.
two。 Modifier class variables and member variables class cAAA {public: cAAA (int a): m_iV (a) {} const int GetValue () const {return void AddValueOneTime () {void AddValueOneTime () {mseciChangeVista;} private: const int msimiliChangeV; static const int massiiStaticV;}; static const int m_iStaticV = 1000 private Const cAAA aa (100); aa.GetValue (); aa.m_iChangeV++
The cAAA class member m_iV is a const variable that must be initialized in the initialization list and cannot be assigned.
For static constant members, similar to ordinary static members, it is recommended to initialize them in .cpp outside the class.
Aa can only call const functions, such as aa.GetValue (), and cannot call the non-member function aa.AddValueOneTime ().
For this kind of const object, if you want to modify the member, you can add mutable to the member declaration, so that the const object aa can also modify m_iChangeV, which is rarely used.
3. Modified member function
Indicates that this function can be called by const objects or ordinary objects without changing the members of the object, that is, it is more like a read-only logic operation, so some people recommend that class member functions can all be added with const. A little trick is to make the non-const version call the const version to avoid code duplication when the const and non-const member functions have substantially equivalent implementations, but not the other way around. Only const functions must be called inside the const function-"Effective C++" clause 03
It is important to note that the compiler enforces bitwase constness, but you should use conceptual constness when writing programs, and the above mutable is used to resolve the compiler's bitwase constness attribute. For more information on the performance of bitwase constness, please refer to Section 03 of "Effective C++".
4. Modifier pointer const char* p1transferchar const * p2terchar * const p3transferConst char* const p4
This is probably a knowledge point that is difficult for beginners to understand. How to distinguish between these four? Remember the secret, read directly from right to left to defeat the enemy in one move.
P1 is a pointer to the char character constant, indicating that the content of the object referred to by p1 cannot be changed and the address can be changed.
P2 and p1 are written differently, and they are equivalent.
P3 is a constant and a pointer to the char character, indicating that the content of the object referred to by p3 can be changed and the address cannot be changed.
P4 is a constant and a pointer to the char character constant, indicating that the content and address of the object referred to by p4 cannot be changed.
Relatively speaking, p1 and p2 are the most commonly used means of passing parameters or returning values.
5. Modifier reference
Modifier references are similar to objects, and the content of the object cannot be changed. As a function parameter, there is no copy overhead, which is recommended, such as: copy constructor, assignment construction, function used for comparison in STL or imitating function. For more information, please see "Effective C++" Section 20. Bool Less (const cAAA& left, const cAAA& right)
Float dValue = 1.05f _ Const int& a = dValue;const int iTemp = dValue;const int& a = iTemp
Because frequent references cannot be changed, in this case the compiler creates a temporary variable to handle the implicit conversion, and we actually make a frequent reference to the temporary variable.
III. Const conversion
Generally speaking, the conversion from T* to const T* is relatively simple, and the implicit conversion supported by the compiler can also be handled with a template, for example, we simply write the RemoveConst template and finally use the alias using. However, it is more troublesome to go from const T* to T*. It is recommended to use const_cast.
Template struct RemoveConst {typedef T Type;}; template struct RemoveConst {typedef T Type;}; template using RCType = typename RemoveConst::Type; IV. Top const and bottom const
To put it simply, the const-decorated object itself cannot be changed is the top-level const, but if the pointer or referenced object cannot be changed, it is called the underlying const.
Const int cV = 10; cV is the top-level const and cannot be changed by itself
Char const * p2; p2 means that the value of the underlying const,p2 itself can be changed, but the content referred to cannot be changed.
The value of char* const p3; p3 is the top-level const,p3 cannot be changed.
Const char* const p4; p4 is both the top const and the bottom const.
Note: the underlying const such as p2 cannot be removed from the above template RCType. If you want to remove it, use const_cast to remove it, but this operation may cause Crash or unknown risks.
Const char* pA = "sss"; char* pB = const_cast (pA); auto pC = RCType (pA); std::cout
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.