FAQ 25.06 Why does the compiler complain about when one template is used inside another?

FAQ 25.06 Why does the compiler complain about >> when one template is used inside another?

Maximal munch.

In the following example, a is a list of vector of int (list and vector are standard container classes; see FAQ 28.13).

 #include <list> #include <vector> using namespace std; int main() {   list< vector<int> >  a;   // ... } 

If the declaration had been written without any spaces between the two > symbols, such as list<vector<int>>, the compiler would have interpreted the two > symbols as a single right-shift operator.

Here are the details. The compiler's tokenizer (something the compiler does to figure out what a program means) has a rule called the maximal munch rule: "Read characters out of the source file until adding one more character causes the current token to stop making sense." For example, the keyword int is one token rather than three separate tokens, i, n, and t. Therefore, if the tokenizer encounters two > symbols together with no whitespace between them, the maximal munch combines them into one token: >>.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net