Hi, the following code is an example of something I'm trying to do in a large project:
#include <iostream>
#include <vector>
// standard template typedef workaround
template<typename T> struct myvar {typedef std::vector<T> Type;};
template<typename T>
T max(typename myvar<T>::Type& x)
// T max(std::vector<T>& x)
{
T y;
y=*x.begin();
for( typename myvar<T>::Type::iterator it=x.begin(); it!=x.end(); ++it )
if( *it>y )
y=*it;
return y;
}
int main(int argc, char **argv)
{
myvar<int>::Type var(3);
var[0]=3;
var[1]=2;
var[2]=4;
std::cout << max(var) << std::endl;
return 0;
}
When I try to compile it I get:
>g++ delme.cpp -o delme
delme.cpp: In function ‘int main(int, char**)’:
delme.cpp:25: error: no matching function for call to ‘max(std::vector<int, std::allocator<int> >&)’
However, if I comment out line 8 and uncomment line 9 it compiles properly and gives:
>g++ delme.cpp -o delme
>./delme
4
Can someone please explain why the function template definition of max() using typename myvar<T>::Type&
isn't considered as a match for ‘max(std::vector<int, std::allocator<int> >&)’
and is there a way to get it to match without using the underlying std::vector<T>&
type?