C
CoolPint
Can anyone explain how I can make the following function accept an
default arguement for the last parameter, which should be an optional
functor?
template <typename T, typename FUNCTOR>
void bsort(T * si, T * ei, FUNCTOR cmpfunc)
{
int k = 0;
for (T * i = si; i < ei - 1; i++, k++)
for (T * j = si; j < (ei-k-1) ; j++)
if ( cmpfunc(*j, *(j+1)) )
swap(*j, *(j+1));
}
Let's say I want to make bsort accept either two or three arguements,
the last
one being optional one which determines the sorting order.
I have tested that overloading the template with another template like
this
template <typename T> void bsort(T *si, T * ei); works! But,
But I am wondering if I can achieve the same effect by providing the
default arguement rather than overloading?
For example, I could define a functor which can be used as the default
argument like below:
template <typename T>
class AscOrder {
public:
bool operator() (T a, T b)
{ return (a >= b ? true : false); }
};
I thought then I could use it as the default argument for the last
parameter like below:
template <typename T, typename FUNCTOR>
void bsort(T *si, T *ei, FUNCTOR cmpfunc = AscOrder<T>() );
But it doesn't work like I thought! The following function call
double dArray[] = {0.5,0.142,0.9,1.5,2.23,2.19,3.5,0.04};
bsort (dArray, dArray+8);
generates compiler error message saying "no matching function for
call..."
What I am misunderstanding here? I would very appreciate if anyone
could kindly explain what I am doing wrong and how I might fix. I just
created this simple self-exercise to help me learn the features of
templates and functors, not to learn how to sort so please no advise
on sorting algorithms. TIA!
default arguement for the last parameter, which should be an optional
functor?
template <typename T, typename FUNCTOR>
void bsort(T * si, T * ei, FUNCTOR cmpfunc)
{
int k = 0;
for (T * i = si; i < ei - 1; i++, k++)
for (T * j = si; j < (ei-k-1) ; j++)
if ( cmpfunc(*j, *(j+1)) )
swap(*j, *(j+1));
}
Let's say I want to make bsort accept either two or three arguements,
the last
one being optional one which determines the sorting order.
I have tested that overloading the template with another template like
this
template <typename T> void bsort(T *si, T * ei); works! But,
But I am wondering if I can achieve the same effect by providing the
default arguement rather than overloading?
For example, I could define a functor which can be used as the default
argument like below:
template <typename T>
class AscOrder {
public:
bool operator() (T a, T b)
{ return (a >= b ? true : false); }
};
I thought then I could use it as the default argument for the last
parameter like below:
template <typename T, typename FUNCTOR>
void bsort(T *si, T *ei, FUNCTOR cmpfunc = AscOrder<T>() );
But it doesn't work like I thought! The following function call
double dArray[] = {0.5,0.142,0.9,1.5,2.23,2.19,3.5,0.04};
bsort (dArray, dArray+8);
generates compiler error message saying "no matching function for
call..."
What I am misunderstanding here? I would very appreciate if anyone
could kindly explain what I am doing wrong and how I might fix. I just
created this simple self-exercise to help me learn the features of
templates and functors, not to learn how to sort so please no advise
on sorting algorithms. TIA!