Template template parameters, or something

  • Thread starter =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
  • Start date
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I've been trying for a while now to understand how template template
parameters work. But I just can't wrap my head around it and was hoping
that someone might help me. As best I can figure the code should look
something like this:

template<template<typename S> typename T>
struct element_traits {
typedef S type;
};

But since it does not work I'd best describe what I want it to
accomplish. If I have a template class A and a template parameter B
(that would be A<B>) I would like the template above to to typedef B to
'type'. So element_traits< A<B> >::type should be of type B.

Question: What am I missing?

Erik Wikström
 
J

John Harrison

Erik said:
I've been trying for a while now to understand how template template
parameters work. But I just can't wrap my head around it and was hoping
that someone might help me. As best I can figure the code should look
something like this:

template<template<typename S> typename T>
struct element_traits {
typedef S type;
};

But since it does not work I'd best describe what I want it to
accomplish. If I have a template class A and a template parameter B
(that would be A<B>) I would like the template above to to typedef B to
'type'. So element_traits< A<B> >::type should be of type B.

Question: What am I missing?

Think of it this way. You are familar with passing a function pointer to
a function. Suppose you did that, like this

void element_traits(void (*t)(int s))

Now suppose you said to yourself, I want to know what the value of s is.

void element_traits(void (*t)(int s))
{
cout << s;
}

I'm sure you know that code doesn't compile, in fact its completely
meaningless.

Well template template parameters are the function pointers of the C++
templates. They stand for a template, but you can't use the parameter S
in your code any more than you can use the parameter s in mine.

John
 
M

mlimber

Erik said:
I've been trying for a while now to understand how template template
parameters work. But I just can't wrap my head around it and was hoping
that someone might help me. As best I can figure the code should look
something like this:

template<template<typename S> typename T>
struct element_traits {
typedef S type;
};

But since it does not work I'd best describe what I want it to
accomplish. If I have a template class A and a template parameter B
(that would be A<B>) I would like the template above to to typedef B to
'type'. So element_traits< A<B> >::type should be of type B.

Question: What am I missing?

In the case you've presented, you don't need template template
parameters. Here's what you need to accomplish your goal:

struct B {};

template<class T>
struct A { typedef T type; };

template<class T>
struct C
{
typedef T::type type;
};

C< A<B> > c;
C< A<B> >::type b;

When you use template template parameters, you intend to specialize the
nested template (in your case T) inside the outer template (in your
code, C). E.g.,

template< template <class> class SomeOtherTemplate >
struct SomeTemplate
{
typedef SomeOtherTemplate<int> IntType;
typedef SomeOtherTemplate<float> FloatType;
typedef SomeOtherTemplate<B> BType;
};

SomeTemplate< A >::IntType aInt;
SomeTemplate< A >::BType b2;

Now A is specialized by SomeTemplate and b2 is of type B.

Cheers! --M
 
G

Greg

Erik said:
I've been trying for a while now to understand how template template
parameters work. But I just can't wrap my head around it and was hoping
that someone might help me. As best I can figure the code should look
something like this:

template<template<typename S> typename T>
struct element_traits {
typedef S type;
};

But since it does not work I'd best describe what I want it to
accomplish. If I have a template class A and a template parameter B
(that would be A<B>) I would like the template above to to typedef B to
'type'. So element_traits< A<B> >::type should be of type B.

Question: What am I missing?

The "S" inside the template template parameter corresponds to no
concrete type. To minimize confusion I would remove it, and just use
<typename>. The only purpose of this inner clause is to specify the
number of parameter types the that the template parameter accepts.

An example might clarify why S is not a type. For the purposes of this
example, we will make a small change to element_traits so that we can
use a std::vector as the parameter:

template<template<typename S, typename> class T>
struct element_traits
{
};

Next, instantiate element_traits:

int main()
{
element_traits< std::vector > vectorTraits;
...

As this example hopefully makes clear, there is no type "S" in
element_traits< std::vector > that can be typedef-ed to "type". A
template template parameter is a template, not a type.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top