473,663 Members | 2,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()(cons t 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>::PMin imum >

I tried putting forward declarations of PQueue template and
PQueue<T>::PMin imum, 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 misunderstandin g 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 2070
CoolPint wrote in news:15******** *************** ***@posting.goo gle.com:

[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>::PMin imum >
Catch 22 applies, PQueue< T > depends on PQueue<T>::PMin imum, the
compiler can't know PQueue<T>::PMin imum 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()(cons t 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()(cons t 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
11360
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 f2(ANY, ANY) ANY f3(ANY, ANY, ANY)
3
2726
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 Foo> class Bar { private: Foo foo;
6
1930
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 comments the comments, but that is not what I want. Since the only different is for the program is either use "vector" "stack" ..., I don't want to specify redundant information "int" overthere.
3
3933
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. template<class Type> class base { public: base& operator/=(const base&); Type *image;
4
1950
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 the reason for doing so. Who can tell me why? Thanks in advance.
5
1739
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; // function object
16
3950
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
5449
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 exists
8
284
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
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.