[计算机科学经

4 template parameter typename 27 templatetypename1

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ; ' '; } std::cout << std::endl; } 在這個 function template ㆗,coll 是個 STL 容器,其元素型別為 T。這裡使用了 STL 容器的迭 代器型別(iterator type)巡訪 coll 的所有元素。迭代器型別為 const_iterator,每㆒個 STL 容器都宣告有這個型別: class stlcontainer { ... typedef ... iterator; // 可讀可寫的迭代器 typedef ... const_iterator; // 惟讀迭代器 ... }; 使用 template type T 的 const_iterator 時 你必須寫出全名 並在最前面加㆖關鍵字 typename , , : typename T::const_iterator pos; .template 構件 ( construct ) 引入關鍵字 typename 之後,㆟們又發現了㆒個類似問題。考慮以㆘程式碼,其㆗使用標準的 bitset 型別: template<int N> void printBitset (std::bitset<N> const& bs) { std::cout << bs.template to_string<char,char_traits<char>,allocator<char> >(); } /* 譯註:你也可以充份運用 bitset 的 member typedef,像這樣: std::cout << bs.template to_string<std::string::value_type, std::string::traits_type, std::string::allocator_type>(); */ C++ Templates - The Complete...
View Full Document

This note was uploaded on 08/25/2012 for the course EECS 101 taught by Professor Wre during the Fall '12 term at Beacon FL.

Ask a homework question - tutors are online