I've been trying out some more advanced template functions recently. As an example I wrote a simple sort routine slightly in the manner of STL:
template <class type> inline void swap(type a[],int e1,int e2){
type t = a[e1];
a[e1] = a[e2];
a[e2] = t;
}
template <class T> struct gt
{
bool operator()(const T& lhs, const T& rhs) const {return lhs>rhs;}
};
template <> struct gt<const char*>
{
bool operator()(const T& lhs, const T& rhs) const {return strcmp(lhs,rhs)>0;}
};
template <class type, class pred> void bubblesort(type a[],int n, pred p)
{
int i,e;
for(i=n;i>0;i--)
for(e=1;e<i;e++)
if(p(a[e-1],a[e])) swap(a,e-1,e);
}
void testBubbleSort()
{
int ai[10], i;
srand((unsigned int)time(NULL));
for(i=0;i<10;i++) ai = rand();
bubblesort(ai,10,gt<int>());
cout << endl;
for (i=0;i<10;i++) cout <<ai << endl;
char* ac[] = {"pqr", "abc","zzz","aaa"};
bubblesort(ac,4,gt<const char*>());
cout << endl;
for (i=0;i<4;i++) cout <<ac << endl;
}
This is all OK, including the specialisation template <> struct gt<const char*>
to handle sorting char*. (This is VC++ 6 SP5 BTW). I then attempted to write a similar specialisation of the STL less struct (actually the point of all this) to handle the same case. (According to Stroustrup this should be in the library, but it doesn't seem to be). This is my code:
template<> struct less<const char* const> : binary_function<const char* const, const char* const, bool>
{
bool operator() (const char* const& _X, const char* const& _Y) const
{ return strcmp(_X, _Y)<0; }
};
void testSTLSort()
{
int i;
char* ac[] = {"pqr", "abc","zzz","aaa"};
sort(ac,ac+4,less<const char* const>());
for (i=0;i<4;i++) cout <<ac << endl;
}
I'm not sure whether that's all correct with the derived class and so on, in any case it does not compile. The message is:
The template name was redefined as a nested element 'struct' of '<unknown>' (more or less, I'm translating from my German compiler's message).
Anyone have any ideas on this?
template <class type> inline void swap(type a[],int e1,int e2){
type t = a[e1];
a[e1] = a[e2];
a[e2] = t;
}
template <class T> struct gt
{
bool operator()(const T& lhs, const T& rhs) const {return lhs>rhs;}
};
template <> struct gt<const char*>
{
bool operator()(const T& lhs, const T& rhs) const {return strcmp(lhs,rhs)>0;}
};
template <class type, class pred> void bubblesort(type a[],int n, pred p)
{
int i,e;
for(i=n;i>0;i--)
for(e=1;e<i;e++)
if(p(a[e-1],a[e])) swap(a,e-1,e);
}
void testBubbleSort()
{
int ai[10], i;
srand((unsigned int)time(NULL));
for(i=0;i<10;i++) ai = rand();
bubblesort(ai,10,gt<int>());
cout << endl;
for (i=0;i<10;i++) cout <<ai << endl;
char* ac[] = {"pqr", "abc","zzz","aaa"};
bubblesort(ac,4,gt<const char*>());
cout << endl;
for (i=0;i<4;i++) cout <<ac << endl;
}
This is all OK, including the specialisation template <> struct gt<const char*>
to handle sorting char*. (This is VC++ 6 SP5 BTW). I then attempted to write a similar specialisation of the STL less struct (actually the point of all this) to handle the same case. (According to Stroustrup this should be in the library, but it doesn't seem to be). This is my code:
template<> struct less<const char* const> : binary_function<const char* const, const char* const, bool>
{
bool operator() (const char* const& _X, const char* const& _Y) const
{ return strcmp(_X, _Y)<0; }
};
void testSTLSort()
{
int i;
char* ac[] = {"pqr", "abc","zzz","aaa"};
sort(ac,ac+4,less<const char* const>());
for (i=0;i<4;i++) cout <<ac << endl;
}
I'm not sure whether that's all correct with the derived class and so on, in any case it does not compile. The message is:
The template name was redefined as a nested element 'struct' of '<unknown>' (more or less, I'm translating from my German compiler's message).
Anyone have any ideas on this?