In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use C++ 's decltype". In the operation of practical 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!
1. What is decltype?
Decltype is a new keyword added to Clover 11, which, like auto, is used for automatic type inference at compile time. Decltype was introduced because auto is not suitable for all automatic type inference scenarios, and in some special cases auto is inconvenient or even impossible to use at all.
For objects of built-in types, using decltype is straightforward, but you should pay attention to some usage details when the parameters are compound types.
Auto varName=value;decltype (exp) varName=value
Auto deduces the type of variable from the initial value on the right side of =, and decltype deduces the type of variable from the exp expression, which has nothing to do with the value on the right side of =.
Auto requires that variables must be initialized, because auto deduces variable types according to their initial values. If they are not initialized, variable types cannot be derived.
Decltype does not require it, so it can be written as follows
Decltype (exp) varName
In principle, exp is just an ordinary expression, which can be in any complex form, but you must ensure that the result of exp is typed, not void;. For example, when exp is a function with a return value of void, the result of exp is also of type void, which will lead to compilation errors.
Several forms of 1.1decltype
Int x = 0 const int type (x) y = 1; / / y-> intdecltype (x + y) z = 0; / z-> intconst int& I = x type (I) j = y; / j-> const int& const decltype (z) * p = & z; / / * p-> const int, p-> const int * decltype (z) * pi = & z / / * pi-> int, pi-> int * decltype (pi) * pp = π / * pp-> int *, pp-> int * * 2. Derivation rule
The derivation rules of decltype can be briefly summarized as follows:
If exp is an expression that is not surrounded by parentheses (), or a class member access expression, or a separate variable, the type of decltype (exp) is the same as exp
If exp is a function call, the type of decltype (exp) is the same as the type of the function's return value
If exp is a left value or is surrounded by parentheses (), the type of decltype (exp) is a reference to exp. Assuming that the type of exp is T, then the type of decltype (exp) is T &.
Example of rule 1:
# include # includeusing namespace std; class A {public: static int total; string name; int age; float scores;} int avatar totalizer 0; int main () {int nudo / Const int & ringing n = An a certain type (n) xroomn; / / n is Int,x is derived as Intdecltype (r) ymetric; / / r is const int &, y is derived as const int & decltype (A::total) zroom0 / total is an int type member variable of class A, z is derived as intdecltype (A.name) url= "www.baidu.com"; / / url is stringleixreturn 0;}
Rule 2 example:
Int& func1 (int, char); / / the return value is int&int&& func2 (void); / / the return value is int&&int func3 (double); / / the return value is int const int& func4 (int,int,int); / / the return value is const int&const int&& func5 (void); / / the return value is const int&&int ndextroutype (func1 (100,100, "A")), and the type of int&decltype (func2 ()) / / the type of int&&decltype (func3 (10.5)) is const int&decltype (func5 ()) and the type of raceme is const int&&.
When calling a function in exp, you need to take parentheses and arguments, but this is only a form and will not actually execute the function code.
Rule 3 example:
The type of class A {public: int x;} int main () {const An obj;decltype (obj.x) is intdecltype ((obj.x)) bachelors; the type of int& int nemesis is int& int nemo, the type of c is intdecltype (n=n+m) d, and the type of d is int& return 0;}
Left value: the data that still exists after the end of expression execution, that is, persistent data; the right value refers to those data that no longer exist at the end of expression execution, that is, temporary data. A simple way to distinguish is to take the address of the expression, which is the left value if the compiler does not report an error, otherwise it is the right value
3. Practical application
Static members of a class can use auto, auto cannot be used for non-static members of a class, and decltype can only be used if you want to deduce the type of a non-static member of a class.
Examples are as follows:
Templateclass A {private: decltype (T.begin ()) masking; / / typename T::iterator masking; / / this usage can lead to errors public:void func (T & container) {m_it=container.begin ();}}; int main () {const vector Vista An obj;obj.func (v); return 0;} that's all for "how to use the decltype of C++". 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.