Templated Functions

[ot] Yeah, I'm still working with Templates and Classes. [/ot]
I've got a class that holds multiple elements and want to sort it in various different ways with the same function. I know the theory behind that.

But I just can't get it written in the good way.

Consider a Class CTest and a random (built-in to semplify) T type.
1
2
3
CTest<T> c;
// Other Initializations
c.Sort< (Type Of Sorting) >();


The "Sort" function looks like:
1
2
3
4
// I think the error resides in the following line:
template < template < typename > typename SortAlg > inline void Sort() {
    SortAlg<T>::Sort( *this );
}


The algorithm baseline looks like:
1
2
3
4
5
6
template <typename T> class DefaultSorting {
    static inline void Sort(CTest<T>& Data)
    {
        // ... The sorting algorithm is protected. Go make your own.
    }
};


I hope I said enough - Reply if I haven't, or Reply if I have, also.
Is it my mistake? Or I just can't do it with " C++03 " ?
I already know I can make the sorting template parameter explicit like...
1
2
3
4
5
template < typename SortAlg > inline void Sort {
    SortAlg::Sort( *this );
}
// ...
c.Sort< (Type Of Sorting)<T> >();

...but that is going to be tedious.

Also this...
1
2
3
4
5
template < typename SortAlg > inline void Sort {
    SortAlg<T>::Sort( *this );
}
// ...
c.Sort< (Type Of Sorting) >();

...isn't good.
Last edited on
So... ¿what's the problem?
c.sort( the_foo_way() );
What is "the_foo_way()" supposed to be? Will it return a function pointer? I was trying to avoid them to get a faster running time, sorting isn't always as fast as anyone wishes and wanted to save that microsecond.

ALSO: Post over edited, little mistake found.

EDIT: Marking question solved, My mistake. The constructor had some troubles.
Last edited on
Not a function pointer, but a functor.
1
2
3
4
5
6
7
class the_foo_way{
public:
   bool operator()(const T &a, const T &b); //as a comparison function
   void sort(container &c); //as an strategy
};
template <class Functor>
void sort( Functor f );

Everything will be resolved at compile time, so you don't need to dereference a function pointer.

I'm a little confused as to what it's supposed to do (a comparison or a sort algorithm).
Besides the resolved question, it was supposed to sort an array of data.
I already did everything anyways, it works well and how I wanted it to.
This can be moved in the archive section.
Topic archived. No new replies allowed.