Connecting Tech Pros Worldwide Forums | Help | Site Map

Template name as template parameter (or something like that)

Jon
Guest
 
Posts: n/a
#1: Nov 17 '08
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.

Pete Becker
Guest
 
Posts: n/a
#2: Nov 17 '08

re: Template name as template parameter (or something like that)


On 2008-11-17 10:19:32 -0500, Jon
<nomorespamplease@nospam-3492hdasuds8d8wsd.netsaid:
Quote:
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)

Jon
Guest
 
Posts: n/a
#3: Nov 17 '08

re: Template name as template parameter (or something like that)


On Mon, 17 Nov 2008 10:32:26 -0500, Pete Becker wrote:
Quote:
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>
Quote:
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.
Quote:
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!
SG
Guest
 
Posts: n/a
#4: Nov 17 '08

re: Template name as template parameter (or something like that)


On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.comwrote:
Quote:
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?
Quote:
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
Pete Becker
Guest
 
Posts: n/a
#5: Nov 17 '08

re: Template name as template parameter (or something like that)


On 2008-11-17 11:11:26 -0500, SG <s.gesemann@gmail.comsaid:
Quote:
On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.comwrote:
Quote:
>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)

Joe Smith
Guest
 
Posts: n/a
#6: Nov 18 '08

re: Template name as template parameter (or something like that)



"Pete Becker" <pete@versatilecoding.comwrote in message
news:2008111715145416807-pete@versatilecodingcom...
Quote:
On 2008-11-17 11:11:26 -0500, SG <s.gesemann@gmail.comsaid:
>
Quote:
>On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.comwrote:
Quote:
>>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.

Pete Becker
Guest
 
Posts: n/a
#7: Nov 18 '08

re: Template name as template parameter (or something like that)


On 2008-11-17 21:17:09 -0500, "Joe Smith" <unknown_kev_cat@hotmail.comsaid:
Quote:
>
"Pete Becker" <pete@versatilecoding.comwrote in message
news:2008111715145416807-pete@versatilecodingcom...
Quote:
>On 2008-11-17 11:11:26 -0500, SG <s.gesemann@gmail.comsaid:
>>
Quote:
>>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)

Closed Thread