473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Questions about defaut template type argument

As a self-exercise, I am trying to write a generic Priority Queue,
which would store any type and and accept any user-definable
"priority" function.

After much tinkering, I came up with something like below:

class PMinimum {
public:
template <typename T>
bool operator()(const T & a, const T & b)
{ return ( a < b ? true : false); }
};

template <typename T, typename F = PMinimum >
class PQueue {
public:
PQueue();
template <typename Iterator>
PQueue(Iterator, Iterator);
bool isEmpty() const;
bool isFull() const;
bool enQueue(const T &);
bool deQueue(T &);
bool getTop(T &);
void clear();
private:
int counter;
DArray<T> heap; // DArray is my own template which works like
<vector>
F cmp;
void buildHeap();
void shiftdown(int);
};

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 >

I tried putting forward declarations of PQueue template and
PQueue<T>::PMinimum, but it still doesn't work. Making it a public
nested class doesn't help either. But I want it to be hidden as a
private member since I believe users of PQueue template doesn't need
to know about it. How can I achieve it?

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?

I tried changing the interface so that the constructor accepts the
"priority" function rather than accepting it as a type argument, but I
cannot make it work to accept both function pointers and functors
either. Making "cmp" a generic type requires a type parameter to be
specified for the PQueue template, which brings me to the same
problem.

I would very much appreciate any help on these two problems. I can
accept second problem might not be solved and only solution is to pass
only functor classes. But if it can be done to accept both normal
function pointers and functors, I would love to learn about it even it
requires many changes to the interface.

I think the first problem is due to my misunderstanding of the
language syntax, feature, etc. So please help me learn to correct my
mistakes. Thank you very much in advance.
Jul 22 '05 #1
2 2054
CoolPint wrote in news:15**************************@posting.google.c om:

[snip]

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 >
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]
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?


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/
Jul 22 '05 #2
> #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;
}


That was so clever! Thank you so much!
This is a kind of eye-opener for me! What books should I read to learn
about techniques like this?
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
6
by: PengYu.UT | last post by:
Hi, I have the following program which use a template as a template parameter. But it seems that it doesn't work. Do you know how to make it work? Although I can change the lines with...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
4
by: Lighter | last post by:
Why is template function not allowed to have defaut arguments? We know that class template is allowed to have default arguments in C++ standard, why is template function not? I can't think out...
5
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; //...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
1
by: subramanian100in | last post by:
consider template<typename TTest { // ... }; We can have a pointer type as argument to a template class. For example, we can have, int x = 100; Test<int*obj(&x); // assuming a suitable ctor...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.