Sunday, January 04, 2009

Compiler issue: Nested template specialization in GCC

(Jan 4, 2009)

I tried to declare a vector that has another container in it. For example,
vector<vector<int>> vecInt;

This syntax works within MS Visual C++, but not in GCC. In GCC, the compiler says
`>>' should be `> >' within a nested template

The above error message is clear. We just need to insert space between angle brackets to fix the issue and change the above statement to
vector< vector<int> > vectInt;

However,
if the inner container is more complicated, the compiler error message
may make you wonder what is happening. For example, if the inner
container is Boost's dynamic bitset:
vector<boost::dynamic_bitset<>> vecBsRoiData;

The error message will be
vecBsRoiData' was not declared in this scope
The
error message is very confusing, but can be handled in the same way by
inserting space between angle brackets and change it to
vector< boost::dynamic_bitset<> > vecBsRoiData;

Note: in reality, we only need to insert space to the closing bracket, but doing so may make the code looks strange. Therefore, I inserted space to both opening and closing brackets.