[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Template name as template parameter (or something like that

Jon

11/17/2008 3:20:00 PM

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 VALUETYPE> class double_map
{
MAPTYPE<KEYTYPE,VALUETYPE> forward_; // for "forward" lookups
MAPTYPE<VALUETYPE,KEYTYPE> reverse_; // 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, int> mymap;

or

double_map<unordered_map, string, int> mymap;

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.
6 Answers

Pete Becker

11/17/2008 3:32:00 PM

0

On 2008-11-17 10:19:32 -0500, Jon
<nomorespamplease@nospam-3492hdasuds8d8wsd.net> said:

> 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 VALUETYPE> class double_map
> {
> MAPTYPE<KEYTYPE,VALUETYPE> forward_; // for "forward" lookups
> MAPTYPE<VALUETYPE,KEYTYPE> reverse_; // 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, class> class 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, int> mymap;

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

Jon

11/17/2008 3:46:00 PM

0

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, class> class 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, int> mymap;

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

11/17/2008 4:11:00 PM

0

On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.com> wrote:
> template <template<class, class> class 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,typename> class MAP,
typename K, typename V>
class D {
MAP<K,V> t1;
MAP<V,K> t2;
};

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

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, int> mymap;

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

11/17/2008 8:15:00 PM

0

On 2008-11-17 11:11:26 -0500, SG <s.gesemann@gmail.com> said:

> On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.com> wrote:
>> template <template<class, class> class 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,typename> class MAP,
> typename K, typename V>
> class D {
> MAP<K,V> t1;
> MAP<V,K> t2;
> };
>
> void fun() {
> // D wants: template<class,class>
> // it gets: template<class,class,class=int>
> D<SuperMap,int,int> d();
> }
>
> 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

11/18/2008 2:17:00 AM

0


"Pete Becker" <pete@versatilecoding.com> wrote in message
news:2008111715145416807-pete@versatilecodingcom...
> On 2008-11-17 11:11:26 -0500, SG <s.gesemann@gmail.com> said:
>
>> On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.com> wrote:
>>> template <template<class, class> class 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,typename> class MAP,
>> typename K, typename V>
>> class D {
>> MAP<K,V> t1;
>> MAP<V,K> t2;
>> };
>>
>> void fun() {
>> // D wants: template<class,class>
>> // it gets: template<class,class,class=int>
>> D<SuperMap,int,int> d();
>> }
>>
>> 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

11/18/2008 12:48:00 PM

0

On 2008-11-17 21:17:09 -0500, "Joe Smith" <unknown_kev_cat@hotmail.com> said:

>
> "Pete Becker" <pete@versatilecoding.com> wrote in message
> news:2008111715145416807-pete@versatilecodingcom...
>> On 2008-11-17 11:11:26 -0500, SG <s.gesemann@gmail.com> said:
>>
>>> On 17 Nov., 16:32, Pete Becker <p...@versatilecoding.com> wrote:
>>>> template <template<class, class> class 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,typename> class MAP,
>>> typename K, typename V>
>>> class D {
>>> MAP<K,V> t1;
>>> MAP<V,K> t2;
>>> };
>>>
>>> void fun() {
>>> // D wants: template<class,class>
>>> // it gets: template<class,class,class=int>
>>> D<SuperMap,int,int> d();
>>> }
>>>
>>> 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)