473,387 Members | 1,520 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,387 software developers and data experts.

Template name as template parameter (or something like that)

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
6 1890
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: littlefitzer | last post by:
Hi, Say for example I want to use the following line in my XSL: <xsl:call-template name="buildText"/> I know I have to have a template defined named buildText. My question is: Is there a way...
2
by: FrankS | last post by:
Hi All, I have a problem with an call-template cmd at xslt 1.0: With: pCall = 'ExInput' ------ I try to: <xsl:call-template name="{$pCall}"> <xsl:with-param name="pVal" select="$pValue"/>...
10
by: philchen1978 | last post by:
Hi, I can compile the code below with GCC 3.4.2, because function g is a "dependent name". template<class T> void f1(T t) { g(t); }
4
by: Keith Chadwick | last post by:
Is it possible to, for example: <xsl:variable name="thetemplate" test="props/prop/@value"/> <xsl:call-template name="$thetemplate"/> I tried {$template} as well. Comes back as invalid qname. ...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
3
by: Fred Kleinschmidt | last post by:
I have a template:: template <class Vclass Vct { public: Vct(V); }; template <class V> Vct<V>::Vct( V vin ) { /*...*/
5
by: raghutumma | last post by:
Hi, I am trying to Pass Column Name(FieldName) using Parameter in SQL Statement... But i am getting error... how can i pass Column name using parameter??? Example: in table i have...
10
by: famat | last post by:
Hi all, I have C++ source code that compiles on windows-MS VS2005 without any problem. I am trying to compile it in Linux gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) and I ran into the following...
1
by: Hari Marimuthu | last post by:
Xslt: What is the difference between Call template and Name template in xslt. Thanks, HariB].
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.