473,769 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template template parameters, or something

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<templa te<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
3 1747
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<templa te<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
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<templa te<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 SomeOtherTempla te >
struct SomeTemplate
{
typedef SomeOtherTempla te<int> IntType;
typedef SomeOtherTempla te<float> FloatType;
typedef SomeOtherTempla te<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
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<templa te<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<templa te<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1531
by: Karthik D | last post by:
Hello All, I am a newbie to C++ code particularly templates.Below is a template class defintion and I couldn't find the ObjPtr/ObjFnPtr class definition anywhere in source code. template<class ObjPtr, class ObjFunPtr> class MemberFunctor0 : public Functor { public: MemberFunctor0(const ObjPtr& obj, const ObjFunPtr& objfn) : _obj(obj), _objfn(objfn)
14
2905
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); template<typename T>
4
2737
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from the start that my knowledge of compiler implementation is very limited. Therefore, my suggestions may have to be rejected because they are difficult or impossible to implement. The proposal is based on the concept of "type similarity". Type...
3
1941
by: Sachin Garg | last post by:
I was trying something with templates in C++, can't figure out if it is possible. Can I check in a function what template parameter was passed, and then decide what to do? for example... I have two simple types:
11
2549
by: Fan Yang | last post by:
I'm reading Modern C++ Design, and it is saying "Variable template parameters simply don't exist." But I find VC7.1 & VC8 support this feature.Who can tell me that which is right -_-b Many thanks.
12
1878
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able to call a method defined in A's base class which at runtime determines the template parameters (I know ahead what is allowed) and calls a templated member function B with A's template parameters.
3
3287
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do this non-intrusively -- I.e., without, for example, typedef'ing the types in every compilation unit? Background: Our code base has assert macros that need to reboot the system after notifying components via a single templatized Component...
4
2061
by: abir | last post by:
I am matching a template, and specializing based of a template, rather than a single class. The codes are like, template<template<typename T,typename Alloc = std::allocator<T> class pix{ }; template<> class pix<vector>{
6
1911
by: Jon | last post by:
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_; //...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.