By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

Template name as template parameter (or something like that)

P: n/a
Jon
Normally I can search and find answers to things like this but with this
one I'm not even sure what to search for and haven't had any luck. Anyway,
I'm trying to use a template name as a template parameter and can't seem
to figure out what I need to do. Code follows:

template <class MAPTYPE, class KEYTYPE, class VALUETYPEclass double_map
{
MAPTYPE<KEYTYPE,VALUETYPEforward_; // for "forward" lookups
MAPTYPE<VALUETYPE,KEYTYPEreverse_; // for "reverse" lookups

....

So I want to use MAPTYPE (which is a template itself) within this template
to specialize on the KEYTYPE and VALUETYPE parameters. For example, want
I want to be able to do something like:

double_map<hash_map, string, intmymap;

or

double_map<unordered_map, string, intmymap;

etc...

I can't seem to work out what I need to do to achieve this and keep the
syntax nice.

Any help would be appreciated.
Nov 17 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 2008-11-17 10:19:32 -0500, Jon
<no**************@nospam-3492hdasuds8d8wsd.netsaid:
Normally I can search and find answers to things like this but with this
one I'm not even sure what to search for and haven't had any luck. Anyway,
I'm trying to use a template name as a template parameter and can't seem
to figure out what I need to do. Code follows:

template <class MAPTYPE, class KEYTYPE, class VALUETYPEclass double_map
{
MAPTYPE<KEYTYPE,VALUETYPEforward_; // for "forward" lookups
MAPTYPE<VALUETYPE,KEYTYPEreverse_; // for "reverse" lookups

...

So I want to use MAPTYPE (which is a template itself)
No, MAPTYPE is the name of a type. That's what the definition of
double_map says. Later in the definition it's used as if it were a
template, which, given the previous definition, is an error.

What you want is for that first parameter to be a template and not a
type. So define the template that way:

template <template<class, classclass maptype, class keytype, class
valuetype...

By the way, hash_map almost certainly won't work here, because it
probably takes more then two template arguments, with all but the first
two having defaults. Welcome to the wonderful world of template
template parameters.

Incidentally, a more verbose approach is used in the standard library
(whose interface pre-dated template template parameters). Use your
original definition, and instantiate it with the right type:

double_map<hash_map<string, int>, string, intmymap;

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 17 '08 #2

P: n/a
Jon
On Mon, 17 Nov 2008 10:32:26 -0500, Pete Becker wrote:
What you want is for that first parameter to be a template and not a
type. So define the template that way:

template <template<class, classclass maptype, class keytype, class
valuetype...
Ah, thanks! I had tried something similar but I was trying to use a name
for other template parameters instead of just template<class,class>
By the way, hash_map almost certainly won't work here, because it
probably takes more then two template arguments, with all but the first
two having defaults. Welcome to the wonderful world of template template
parameters.
Yep, you're right. Wow this is actually pretty nasty because I'd rather
use the default values of whatever template type is specified. Not so
straightforward and a little too verbose for what I wanted.
Incidentally, a more verbose approach is used in the standard library
(whose interface pre-dated template template parameters). Use your
original definition, and instantiate it with the right type:

double_map<hash_map<string, int>, string, intmymap;
The problem with this is that it would actually need the reverse type too:

double_map<hash_map<string, int>, hash_map<int, string>, string, int>
mymap;

_Really_ verbose and annoying for sure. :)

Thanks!
Nov 17 '08 #3

P: n/a
SG
On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.comwrote:
template <template<class, classclass maptype, class keytype, class
valuetype...

By the way, hash_map almost certainly won't work here, because it
probably takes more then two template arguments, with all but the first
two having defaults. Welcome to the wonderful world of template
template parameters.
G++ seems to like the following code even with -Wall and -pedantic. In
the function "fun" the template class D ist instantiated with
"SuperMap" as a template template parameter that has a third template
parameter with a default (T3 = int).

template<typename KEY, typename MTYPE, typename T3 = int>
class SuperMap {};

template< template<typename,typenameclass MAP,
typename K, typename V>
class D {
MAP<K,Vt1;
MAP<V,Kt2;
};

void fun() {
// D wants: template<class,class>
// it gets: template<class,class,class=int>
D<SuperMap,int,intd();
}

Is this really not supported by the C++ standard officially?
Incidentally, a more verbose approach is used in the standard library
(whose interface pre-dated template template parameters). Use your
original definition, and instantiate it with the right type:

double_map<hash_map<string, int>, string, intmymap;
In that case you need some kind of "rebinding" functionality
(MAPTYPE::rebind<VAL,KEY>::type) to make it work. --- or just another
template parameter for the 2nd map type.
Cheers!
SG
Nov 17 '08 #4

P: n/a
On 2008-11-17 11:11:26 -0500, SG <s.********@gmail.comsaid:
On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.comwrote:
>template <template<class, classclass maptype, class keytype, class
valuetype...

By the way, hash_map almost certainly won't work here, because it
probably takes more then two template arguments, with all but the first
two having defaults. Welcome to the wonderful world of template
template parameters.

G++ seems to like the following code even with -Wall and -pedantic. In
the function "fun" the template class D ist instantiated with
"SuperMap" as a template template parameter that has a third template
parameter with a default (T3 = int).

template<typename KEY, typename MTYPE, typename T3 = int>
class SuperMap {};

template< template<typename,typenameclass MAP,
typename K, typename V>
class D {
MAP<K,Vt1;
MAP<V,Kt2;
};

void fun() {
// D wants: template<class,class>
// it gets: template<class,class,class=int>
D<SuperMap,int,intd();
}

Is this really not supported by the C++ standard officially?
See the example in [temp.arg.template] /2.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 17 '08 #5

P: n/a

"Pete Becker" <pe**@versatilecoding.comwrote in message
news:2008111715145416807-pete@versatilecodingcom...
On 2008-11-17 11:11:26 -0500, SG <s.********@gmail.comsaid:
>On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.comwrote:
>>template <template<class, classclass maptype, class keytype, class
valuetype...

By the way, hash_map almost certainly won't work here, because it
probably takes more then two template arguments, with all but the first
two having defaults. Welcome to the wonderful world of template
template parameters.

G++ seems to like the following code even with -Wall and -pedantic. In
the function "fun" the template class D ist instantiated with
"SuperMap" as a template template parameter that has a third template
parameter with a default (T3 = int).

template<typename KEY, typename MTYPE, typename T3 = int>
class SuperMap {};

template< template<typename,typenameclass MAP,
typename K, typename V>
class D {
MAP<K,Vt1;
MAP<V,Kt2;
};

void fun() {
// D wants: template<class,class>
// it gets: template<class,class,class=int>
D<SuperMap,int,intd();
}

Is this really not supported by the C++ standard officially?

See the example in [temp.arg.template] /2.
The example you are referring to is not present in C++03. It is present in
the lastest working draft.

Nov 18 '08 #6

P: n/a
On 2008-11-17 21:17:09 -0500, "Joe Smith" <un*************@hotmail.comsaid:
>
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:2008111715145416807-pete@versatilecodingcom...
>On 2008-11-17 11:11:26 -0500, SG <s.********@gmail.comsaid:
>>On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.comwrote:
template <template<class, classclass maptype, class keytype, class
valuetype...

By the way, hash_map almost certainly won't work here, because it
probably takes more then two template arguments, with all but the first
two having defaults. Welcome to the wonderful world of template
template parameters.

G++ seems to like the following code even with -Wall and -pedantic. In
the function "fun" the template class D ist instantiated with
"SuperMap" as a template template parameter that has a third template
parameter with a default (T3 = int).

template<typename KEY, typename MTYPE, typename T3 = int>
class SuperMap {};

template< template<typename,typenameclass MAP,
typename K, typename V>
class D {
MAP<K,Vt1;
MAP<V,Kt2;
};

void fun() {
// D wants: template<class,class>
// it gets: template<class,class,class=int>
D<SuperMap,int,intd();
}

Is this really not supported by the C++ standard officially?

See the example in [temp.arg.template] /2.
The example you are referring to is not present in C++03. It is present
in the lastest working draft.
Whoops, sorry. See the example in [temp.arg.template]/2 in the current
working draft. <g>

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 18 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.