Iterator over a map of generic vectors.

Ravishankar S ravishankar.s@hathway.com
Mon Sep 11 15:32:00 GMT 2006


Hi,


This must be asked of a C++ expert, but let me guess from compiler
implementation. 

Correct me if im wrong, I think the compiler cannot decide how to
specialize based on the types of local variable, it requires the types
of function arguments, to do this. 

For example:

tempale<typename T>
void function(T& a,T b,T *c)
{
  T v1,v1,v3;

  v1 = a
  v2 = b;
  b3 = *c;
}

function(int&,int,int*) if T == int will be distinguised from other
values for T.

I don't know how it compiled on VC++ ! So I tried it out. It compiles
but does not link.

#include <iostream.h>
#include <iomanip>
#include <map>

template<typename T>
void func()
{
    map< int, vector<T> > m;             //we want an iterator over m.
    map< int, vector<T> >::iterator i;      //this doesn't compile
    map< int, vector<int> >::iterator k;   //but this does

    cout << "In func()\n" << endl;
}

int main(void)
{
  func();
  return 0;
}

c:\documents and settings\ravishankar\test.cpp(17) : error C2783: 'void
__cdecl func(void)' : could not deduce template argument for 'T'


Regards,
Ravishankar








-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Eric Lance
Sent: Saturday, September 09, 2006 3:59 AM
To: gcc-help@gcc.gnu.org
Subject: Iterator over a map of generic vectors.


Hi all,

I'm trying to figure out if this is a bug or "feature". In a generic
function, I want to declare an iterator over a map whose values are
generic vectors. However, I get a syntax error whenever I try to declare
this particular kind of iterator. The code snippet below should
illustrate what I mean better:

template<typename T>
void func()
{
    map< int, vector<T> > m;             //we want an iterator over m.
    map< int, vector<T> >::iterator i;      //this doesn't compile
    map< int, vector<int> >::iterator k;   //but this does
}

On GCC 4.1.0 (Suse) and GCC 3.4.4 (FreeBSD) it throws the following
syntax error:
      Test.cpp: In function 'void func()':
      Test.cpp:16: error: expected `;' before 'i'

The code snippet will compile cleanly on MS visual studio.  It also
seems illogical that I can declare map "m", but it is illegal to declare
the iterator " i". So, is this a "feature" or a bug?


I'm not subscribed to the list, so please CC me in on any replies.

Thanks,
Eric


PS As a workaround, I changed the declaration to the following, which
seems to work:
    typeof(m.iterator) i=m.begin();

__________ NOD32 1.1454 (20060321) Information __________

This message was checked by NOD32 antivirus system. http://www.eset.com




More information about the Gcc-help mailing list