473,473 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template Template Parameters

First of all, thanks mlimber for answering my previous question. Now...

I'm following the example in "C++ Templates: The Complete Guide",
section 5.4 Template Template Parameters, and I'm wondering if it's
basics/stack8.hpp example can be compiled under g++ 4.

First a bit background about template template:

5.4 Template Template Parameters

It can be useful to allow a template parameter itself to be a class
template. Again, our stack class template can be used as an example. To
use a different internal container for stacks, the application programmer
has to specify the element type twice. Thus, to specify the type of the
internal container, you have to pass the type of the container and the
type of its elements again:

Stack<int,std::vector<int vStack; // integer stack that uses a vector

Using template template parameters allows you to declare the Stack class
template by specifying the type of the container without respecifying the
type of its elements:

stack<int,std::vectorvStack; // integer stack that uses a vector

To do this you must specify the second template parameter as a template
template parameter...

To solve it, the book proposed the following solution (extracted to
preserve problem only):

$ cat -n templ-templ.cc
1 #include <memory>
2
3 template< typename ALLOC = std::allocator<int
4 class c {};
5
6 template <typename T,
7 template <typename ELEM,
8 typename ALLOC = std::allocator<ELEM
9 class CONT = std::deque>
10 class Stack { };
11
12 int main()
13 {
14 }
However, it doesn't compile under my g++:

$ g++ --version
g++ (GCC) 4.2.3 (Debian 4.2.3-1)

$ g++ -g templ-templ.cc -o templ-templ
templ-templ.cc:9: error: invalid default argument for a template template parameter

Anyone can help make it compile?

Thanks

tong
Aug 12 '08 #1
4 3010
* Tong * wrote:
To solve it, the book proposed the following solution (extracted to
preserve problem only):

$ cat -n templ-templ.cc
1 #include <memory>
2
3 template< typename ALLOC = std::allocator<int
4 class c {};
5
6 template <typename T,
7 template <typename ELEM,
8 typename ALLOC = std::allocator<ELEM
9 class CONT = std::deque>
10 class Stack { };
11
12 int main()
13 {
14 }
However, it doesn't compile under my g++:

$ g++ --version
g++ (GCC) 4.2.3 (Debian 4.2.3-1)

$ g++ -g templ-templ.cc -o templ-templ
templ-templ.cc:9: error: invalid default argument for a template
template parameter

Anyone can help make it compile?
What about adding an

#include <deque>

to tell the compiler what the default parameter is?
You might also want to check Josuttis' list of known typos in the
book, like <memoryinstead of <allocator>, at this address:

http://www.josuttis.com/tmplbook/
Bo Persson
Aug 12 '08 #2
On Wed, 13 Aug 2008 01:32:51 +0200, Bo Persson wrote:
>templ-templ.cc:9: error: invalid default argument for a template
template parameter

Anyone can help make it compile?

What about adding an

#include <deque>

to tell the compiler what the default parameter is?
yes, yes, silly me.

Thanks.
Aug 13 '08 #3
Invalid default argument for a template template is caused by these
two lines as below:

template <typename ALLOC = std::alocator<int
class C;

This default template argument need <memoryheader file
template <typename T,

template <
typename ELEM,
typename ALLOC = std::allocator<ELEM>
>,
class CONT = std::deque
>
I wonder how this can compile under standard C++ compiler.

A template inside a template parameter.

How to achieve this.

Thanks.

Aug 14 '08 #4
Pe********@gmail.com wrote:
Invalid default argument for a template template is caused by these
two lines as below:

template <typename ALLOC = std::alocator<int
class C;

This default template argument need <memoryheader file
template <typename T,

template <
typename ELEM,
typename ALLOC = std::allocator<ELEM>
>,
class CONT = std::deque
>

I wonder how this can compile under standard C++ compiler.

A template inside a template parameter.

How to achieve this.
That's the whole point of a template template parameter.
Aug 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gianni Mariani | last post by:
I was a little surprised by this: It seems like the code below should not compile but the Comeau 4.3.3 compiler accepts it and the gcc 3.4(prerel) compiler rejects it and MSVC++7.1 ICE's. ...
11
by: Alexander Stippler | last post by:
Hi, I have a little problem to design some template classes in a realizable way. I have some Container classes and some Proxy classes (proxies for elements). Looks like this: // P is the...
3
by: Erik Wikström | last post by:
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...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
2
by: Pierre Yves | last post by:
Hi there, Sorry for the double subject but I feel they are related. I'm not pretty sure there would be an answer but I reckon there must be a way to make it work. I would like to write the...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.