# C++ question: nesting template classes

**URL:** <https://boards.straightdope.com/t/c-question-nesting-template-classes/157674>\
**Category:** Factual Questions\
**Created:** [February 26, 2003, 5:17pm UTC](https://boards.straightdope.com/t/c-question-nesting-template-classes/157674 "2003-02-26T17:17:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [February 26, 2003, 5:17pm UTC](https://boards.straightdope.com/t/c-question-nesting-template-classes/157674/1 "2003-02-26T17:17:21Z")

</div>

I’m trying to create a 2D sparse array with elements of type set\<char\*\> (that’s the STL set class). I’ve got a template class Cell\<T\> (T is some type) that would be perfect for implementing this.

Both classes work fine on their own, but I can’t compose them. That is, Cell\<char\*\> works, and set\<char\*\> works, but Cell\<set\<char\*\>\> doesn’t work. And I need Cell\<Cell\<set\<char\*\>\>\> to work. Shouldn’t that work? Am I running up against some obscure language constraint?

This is in MS VC6. Switching over to anything other than [MS.net](http://MS.net) would be impractical, cause we’re an MS shop.

Any help would be immensely appreciated.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [February 26, 2003, 5:19pm UTC](https://boards.straightdope.com/t/c-question-nesting-template-classes/157674/2 "2003-02-26T17:19:04Z")

</div>

Forgot to mention the errors I’m getting. The offending line is “Cell\<Cell\<set\<char\*\>\>\>\* m\_SparseTable;”, and I’m getting the following errors:

error C2146: syntax error : missing ‘,’ before identifier ‘m\_SparseTable’  
error C2065: ‘m\_SparseTable’ : undeclared identifier  
error C2143: syntax error : missing ‘\>’ before ‘;’  
error C2208: ‘class Cell’ : no members defined using this type

---

<div class="post-metadata">

**Author:** ![ChordedZither](https://avatars.discourse-cdn.com/v4/letter/c/e9c0ed/32.png) [@ChordedZither](https://boards.straightdope.com/u/ChordedZither)\
**Post date:** [February 26, 2003, 5:47pm UTC](https://boards.straightdope.com/t/c-question-nesting-template-classes/157674/3 "2003-02-26T17:47:19Z")

</div>

Try  
Cell\<set\<char\*\> \>  
That is, you need a space between successive ‘\>’ characters.

If you run them together, it becomes the \>\> operator, as in  
cin \>\> input1 \>\> input2;

---

<div class="post-metadata">

**Author:** ![sinemora](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@sinemora](https://boards.straightdope.com/u/sinemora)\
**Post date:** [February 26, 2003, 5:47pm UTC](https://boards.straightdope.com/t/c-question-nesting-template-classes/157674/4 "2003-02-26T17:47:43Z")

</div>

This is a known limitation of the C++ grammar. The tokenizer is confused about the \> signs. Ideally, you want them to be parsed as 3 greater than tokens. However, \>\> is also a valid operator itself. When you have \>\>\> without any spaces between them, by the longest match rule, they will be interpreted as one “right shift” and one “greater than” instead of 3 greater than.

Solution 1: Insert spaces. For example,

Cell\< set\<char \*\> \> tbl;

Solution 2: Use typedef.

typedef set\<char \*\> char\_set\_t;  
typedef Cell\<char\_set\_t\> char\_cell\_t;

char\_cell\_t tbl;

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [February 26, 2003, 5:52pm UTC](https://boards.straightdope.com/t/c-question-nesting-template-classes/157674/5 "2003-02-26T17:52:17Z")

</div>

The spaces are easier on me, so that’s what I’ll do. Thanks a bunch.
