Re: template iterator
On Jan 30, 4:50 pm, "Doug Harrison [MVP]" <d...@mvps.org> wrote:
On Wed, 30 Jan 2008 16:26:12 -0800 (PST), b...@coolgroups.com wrote:
Why doesn't this compile in VS 2005?
#include "stdafx.h"
#include <vector>
using namespace std;
template < typename ty >
class MyTemplateClass
{
vector<ty> a;
vector<ty>::iterator b;
};
int _tmain(int argc, _TCHAR* argv[])
{
MyTemplateClass<int> test;
return 0;
}
The "iterator" in vector<ty>::iterator depends on a template parameter and=
is thus said to be a "dependent type". You have to tell the compiler that
"iterator" really is a type as opposed to a non-type member of vector<ty>.=
You do this with the typename keyword:
typename vector<ty>::iterator b;
This is required because vector<ty> could be specialized at the point of
instantiation of MyTemplateClass<ty>, such that iterator could be a type o=
r
non-type, irrespective of its definition in the primary vector template. B=
y
default, the compiler assumes it's a non-type, and using the typename
keyword overrides this default.
--
Doug Harrison
Visual C++ MVP- Hide quoted text -
- Show quoted text -
Ok, it compiles now. Out of curiosity, why does the original code
compile in both VC++ 6.0 and Dev-C++ (but not VS 2005)? Is it just a
difference in default assumptions?