Template issue with vector [duplicate]

2019-03-04 17:39发布

问题:

Possible Duplicate:
For nested templates, when did `>>` become standard C++ (instead of `> >`)?
Why did templates of templates (e.g. vector<vector<int> >) require a space between the closing angle brackets prior to C++0x?

I am simply trying to create a vector:

vector<Transform3D<double>> tempVector;

This is the compilation error i get:

/../main.cpp:34:26: error: a space is required between consecutive right angle brackets
  (use '> >')
vector<Transform3D<double>> tempVector;
                         ^~
                         > >

What does not make is sense is, why the problem is solved by changing the vector to as the error describes:

vector<Transform3D<double > > tempVector;

Why is vector<Transform3D<double>> and vector<Transform3D<double > > not identical?

回答1:

They aren't identical (at least prior to C++11) because the last >> characters are parsed as a single operator (operator>>). Putting a space between them causes the expected behavior.

The same situation happens where the compiler parses <: as the beginning of a tigraph/digraph. For example:

N<::T> // <: parsed as [

A space separating the operators causes the code to work fine.



回答2:

Because >> is a bit shift operator, a newer compiler can distinguish the two though.



标签: c++ vector