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

Template template parameters, or something

P: n/a
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
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Nov 10 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Erik Wikström wrote:
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
Nov 10 '05 #2

P: n/a
Erik Wikström wrote:
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

Nov 10 '05 #3

P: n/a
Erik Wikström wrote:
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

Nov 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.