In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the role of the constexpr constructor in Category 11. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.
Polymeric class
Aggregate class allows users to directly access their members and has a special initialization form. The aggregation class meets the following conditions:
All members are public.
No constructor defined
No in-class initial value
No base class, no virtual function.
How do you understand it?
First of all, it seems that the aggregate class is actually a C structure; secondly, the word aggregation should be relatively combined, indicating the loose relationship between members and classes.
When a class is an aggregate class, it can be initialized using the initial value list as follows:
Struct Point {
Int x
Int y
}
Point pt = {10,10}
Literal constant class
I talked about the constexpr function earlier, and its arguments and return values must be constant expressions. The most basic element of a constant expression is the literal type. In addition to literal types, including arithmetic types, references and pointers, some classes also belong to literal types, which are called literal constant classes. There are mainly two situations:
First of all, the aggregate class in which the data members are literal is one. The Point class above is an example. We can understand that the aggregation of literals continues to have the characteristics of literals, which can be evaluated at compile time.
In another case, although it is not an aggregate class, it is also a literal constant class as long as the following conditions are met:
Data members must all be literal types.
Class must contain at least one constexpr constructor.
If a data member contains an in-class initial value, the initial value must be a constant expression; if the member belongs to a class, the initial value must use the class's constexpr constructor.
Class must use the default definition of the destructor.
The author understands these conditions as follows:
If condition 1 is met, it can be evaluated at compile time, just like aggregate classes.
If condition 2 is met, you can create an object of type constexpr for this class.
If condition 3 is met, it can be guaranteed that even if there is in-class initialization, it can be solved during the compilation phase.
If condition 4 is satisfied, no unexpected operation of the destructor can be guaranteed.
Constexpr constructor
By preposing the constexpr keyword, you can declare the constexpr constructor, while:
Except for declaring as = default or = delete, the function body of the constexpr constructor is generally empty, initializing all data members with an initialization list or other constexpr constructor.
Struct Point {
Constexpr Point (int _ x, int _ y)
X (_ x), y (_ y) {}
Constexpr Point ()
: Point (0d0) {}
Int x
Int y
}
Constexpr Point pt = {10,10}; this is what the role of the constexpr constructor in the shared 11 is. If you happen to have similar doubts, please refer to the above analysis to understand it. If you want to know more about it, you are welcome to follow the industry information channel.
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.