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 > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the sample analysis of class and typename in the C++ template. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
The difference between using class and typename in templates
I haven't turned a few pages yet, and I froze when I saw this code. As far as I can remember, I didn't understand it here last time, and I searched it specially. And I forgot when I did it again. Sure enough, a good memory is not as good as a bad pen. Write a blog mark as soon as possible.
What I'm talking about here is that the author uses typename instead of class when declaring templates. In general, using typename or class is just a matter of coding style. But when it comes to subordinate types (dependent type), in order to avoid potential parsing ambiguity, you must use typename instead of class.
Templatebool lastGreaterThanFirst (const C & container) {if (container.empty ()) return false; typename C::const_iterator begin (container.begin ()); typename C::const_iterator end (container.end ()); return *-- end > * begin;}
The focus here is on these two lines:
Typename C::const_iterator begin (container.begin ()); typename C::const_iterator end (container.end ())
If you do not use the typename keyword
Templatebool lastGreaterThanFirst (const C & container) {if (container.empty ()) return false; C::const_iterator begin (container.begin ()); C::const_iterator end (container.end ()); return *-- end > * begin;}
An error will be reported ("Effective STL" indicates that some compilers mistakenly accept code without typename, but such code is not portable):
Test.cpp:6:2: error: missing 'typename' prior to dependent typename' C::const_iterator begin (container.begin ()); ^ ~ typenametest.cpp:7:2: error: missing' typename' prior to dependent typename' C::const_iterator end (container.end ()); ^ ~ typename2 errors generated.
If you use class instead of typename here, you will get an error:
Test.cpp:8:11: error: elaborated type refers to a typedef class C::const_iterator begin (container.begin ()); ^ test.cpp:15:2: note: in instantiation of function template specialization 'lastGreaterThanFirst' requested here lastGreaterThanFirst (vec); ^ / Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:476:54: note: declared here typedef _ _ wrap_iter const_iterator; ^ 1 error generated.
Why do you need to use typename when there are dependent types
Let's analyze it step by step.
ClassA::foo
When you see the code above, what do you think foo is? The first reaction should be a static member variable of ClassA, right?
So when you look back and see the following code, think about what an is?
ClassA::foo a
An is a variable of type ClassA::foo, and ClassA::foo is an inner class:
Class ClassA {public: class foo {};}
Or a typedef inside ClassA:
Class ClassA {public: typedef int foo;}
When foo is an inner class of ClassA or an internal typedef, foo is a dependent type.
However, as for the CRARV interpreiterator, which may be a static member variable of C or a dependent type of C, the compiler parses it to a variable by default, so you need to use typename to tell the compiler that this is a type:
Typename C::const_iterator begin (container.begin ())
A special case where typename is not required when a dependent type occurs
When you encounter a base class list of dependent types that appear in the class template definition, you do not need to use the typename keyword to indicate that this is a type:
Class ClassA {public: class foo {};}; templateclass ClassB: public C::foo {}
Because the one in the base class list must be a type.
This is the end of this article on "sample Analysis of class and typename in C++ template". 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, please 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.