C ++ template set Template

Source: Internet
Author: User

1. The template class has members who are templates.

 
# Include <iostream> # include <typeinfo> using namespace STD; Template <class T> class Outer {public: Template <class r> class inner {public: void F () ;};}; template <class T> template <class r> void outer <t >:: inner <r >:f () {cout <"outer =" <typeid (t ). name () <Endl; cout <"inner =" <typeid (R ). name () <Endl; cout <"full inner =" <typeid (* This ). name () <Endl ;}int main () {outer <int >:: inne R <bool> Inner; Inner. F ();}/**////:~ // Output: // outer = int // inner = bool // full inner = Class outer <int> :: inner <bool> // press any key to continue...

2. template parameters

// A print function for Standard C ++ sequences # include <iostream> # include <list> # include <memory> # include <vector> # include <deque> using namespace STD; template <class T, template <Class U, class = Allocator <u> class seq> void printseq (SEQ <t> & SEQ) {for (typename seq <t> :: iterator B = seq. begin (); B! = Seq. end ();) cout <* B ++ <Endl ;}int main () {// process a vector <int> V; V. push_back (1); V. push_back (2); printseq (V); // process a list <int> lst; lst. push_back (3); lst. push_back (4); printseq (LST); // process a deque <int> D; D. push_back (5); D. push_back (6); printseq (d );}/**////:~

Note: typename notifies the compiler that the qualified identifier should be of type. Different from typedef, the new type is defined.

Original article address

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.