CoolPint wrote in news:159a5c32.0312192056.4f21bf12@posting.google.c om:
[snip]
[color=blue]
>
> I tested it and it works fine, but there are two things which I cannot
> do.
>
> First, how can I hide class PMinimum as a private member of PQueue
> template?
> If I bring the class definition as a private member, how do I specify
> it as the default template type argument? I tried below but it won't
> compile
>
> template <typename T, typename F = PQueue<T>::PMinimum >[/color]
Catch 22 applies, PQueue< T > depends on PQueue<T>::PMinimum, the
compiler can't know PQueue<T>::PMinimum till it knows (instantiats)
PQueue<T>.
[snip]
[color=blue]
> Secondly, I noticed that PQueue works only if Functor classes are
> provided as the type argument to the second type parameter. For
> example, follwoing
> PQueue<double, Priority> pdq;
> would work only if "Priority" is a name of Functor class. Is it
> possible to make it accept both normal functions (function pointers)
> as well as Functors?[/color]
you need to give the function type say bool (*)(int, int) as the type
argument and have ctor that take such a function-pointer.
[snip]
#include <iostream>
#include <ostream>
#include <set>
template < typename NonVoid, typename Alt >
struct select_non_void
{
typedef NonVoid type;
};
template < typename Alt >
struct select_non_void< void, Alt >
{
typedef Alt type;
};
template < typename A, typename B = void >
struct PQ
{
private:
class PMinimum {
public:
template <typename T>
bool operator()(const T & a, const T & b)
{ return ( a < b ? true : false); }
};
typedef typename select_non_void< B, PMinimum >::type comp_t;
comp_t comp;
public:
template < typename T >
bool test( T const &l, T const &r )
{
return comp( l, r );
}
PQ( comp_t c ) : comp( c ) {}
PQ() {}
};
bool comp_int( int a, int b )
{
return a > b;
}
int main()
{
using namespace std;
PQ< int > pq;
cout << pq.test( 1, 2 ) << endl;
PQ< int, bool (*)(int, int) > pq2( comp_int );
cout << pq2.test( 1, 2 ) << endl;
}
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/