Topic: VC++ version 5 template function problem (that works with g++)


Author: petey@best.com (Roger Reynolds)
Date: 1997/05/28
Raw View
Here is a trivial little test program, that illustrates
different behavior between g++ (2.7.2.1) and Visual C++ 5.0
when trying to use a template function with a list of the
template paramater type...

/////////
#ifdef _AFXDLL
#include <afxwin.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include <list>
#ifndef __GNUG__
using namespace std;
#endif

template <class T>
void ZZZ (list<T*> tlist)
{
    cerr << "ZZZ size = " << tlist.size() << endl;
}

int main (int, char**)
{
    list<int*>f;
    f.push_front (new int);
    f.push_front (new int);
    ZZZ(f);
    return 0;
}
/////////

Now this program compiles and runs correctly using g++
(prints ZZZ size = 2), but with VC++, I get the following compile error:

error C2664: 'ZZZ' : cannot convert parameter 1 from
  'class std::list<int *, class std::allocator<int*>>' to
  'class std::list<int * *, class std::allocator<int* *>>'

So, it looks like VC has incorrectly added a layer of pointer
indirection when expanding the template definition, or I am
misunderstanding the error message.

So, I guess my question is, should this work in VC or not work in g++?

And a reality check...
The specific goal of this is to create a template function which iterates
over a list<whatever*>, calling whatever->method for each item,
which in this case is  operator >> or <<.
i.e. print all the elements in a list

If there is a better way to do that sort of thing than the
approach I'm taking, I'd sure like to hear some suggestions..

Thanks alot
Roger - rsr@rogerware.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]