How to specify a template as the typename of a template



 DEVELOP > c-Plus-Plus > How to specify a template as the typename of a template

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 09 May 2005 05:16:33 PM
Object: How to specify a template as the typename of a template
Hi,
I have the following program which use a template as a template
parameter. But it seems that it doesn't work. Do you know how to make
it work?
Although I can change the lines with comments the comments, but that is
not what I want. Since the only different is for the program is either
use "vector" "stack" ..., I don't want to specify redundant information
"int" overthere.
Best wishes,
Peng
#include <iostream>
#include <vector>
template <typename __Tp>
class A{
public:
A(){};
~A(){};
void push_back(int i){ _vector_int_inst.push_back(i);}
void show(){
copy(_vector_int_inst.begin(), _vector_int_inst.end(),
ostream_iterator<int>(s
td::cout, "\n"));
}
private:
__Tp<int> _vector_int_inst;//__Tp _vector_int_inst;
};
int main(int argc, char *argv[])
{
A<std::vector> a;//A<std::vector<int> > a;
a.push_back(1);
a.push_back(2);
a.show();
}
.

User: "red floyd"

Title: Re: How to specify a template as the typename of a template 09 May 2005 05:57:31 PM
wrote:

Hi,

I have the following program which use a template as a template
parameter. But it seems that it doesn't work. Do you know how to make
it work?

You want a template template parameter

Although I can change the lines with comments the comments, but that is
not what I want. Since the only different is for the program is either
use "vector" "stack" ..., I don't want to specify redundant information
"int" overthere.

Best wishes,
Peng

#include <iostream>
#include <vector>

template <typename __Tp>

template <template <typename T> typename __Tp>

class A{
public:
A(){};
~A(){};
void push_back(int i){ _vector_int_inst.push_back(i);}
void show(){
copy(_vector_int_inst.begin(), _vector_int_inst.end(),
ostream_iterator<int>(s
td::cout, "\n"));
}
private:
__Tp<int> _vector_int_inst;//__Tp _vector_int_inst;
};

int main(int argc, char *argv[])
{
A<std::vector> a;//A<std::vector<int> > a;
a.push_back(1);
a.push_back(2);
a.show();
}

Note: you should not use __Tp as a name. Any identifier containing two
consecutive underscores is reserved to the implementation.


.
User: ""

Title: Re: How to specify a template as the typename of a template 09 May 2005 09:27:17 PM
Can I use an identifier beginning with one underscore?
Best wishes,
Peng
.
User: "Victor Bazarov"

Title: Re: How to specify a template as the typename of a template 09 May 2005 09:55:45 PM
wrote:

Can I use an identifier beginning with one underscore?

Why?
.
User: ""

Title: Re: How to specify a template as the typename of a template 09 May 2005 10:15:32 PM
I just want to whether it is legal or not. Because I already have some
code which have identifiers starting with undersore.
Peng
.
User: "Victor Bazarov"

Title: Re: How to specify a template as the typename of a template 10 May 2005 08:17:45 AM
wrote:

I just want to whether it is legal or not. Because I already have some
code which have identifiers starting with undersore.

Identifiers that contain double underscores or that begin with
an underscore followed by a capital letter, are reserved. Just
avoid leading underscores altogether.
V
.





User: "Victor Bazarov"

Title: Re: How to specify a template as the typename of a template 09 May 2005 06:03:34 PM
wrote:

I have the following program which use a template as a template
parameter. But it seems that it doesn't work. Do you know how to make
it work?

Although I can change the lines with comments the comments, but that is
not what I want. Since the only different is for the program is either
use "vector" "stack" ..., I don't want to specify redundant information
"int" overthere.

Best wishes,
Peng

#include <iostream>
#include <vector>

template <typename __Tp>

First of all, drop the double underscore from here. You are not allowed
to use reserved names.
Second, if you intend to give your Tp a template argument, like you do
below, Tp is not a type, it's a template. You need to declare it as such:
template<template<class> class Tp>
It's called "template template argument", read about them in a good book
on C++ templates.
Third, if you intend to pass 'std::vector' there, you will not be able to
do so because 'std::vector' actually has more than one argument and has
to be specified as
template<template<class,class> class Tp>
or even with three arguments. The problem is that our common use of the
'std::vector' template involves using the _default_ arguments for all but
the first template argument.
To solve that I use policies:
template<class T> struct use_vector { typedef std::vector<T> type; };
template<class T> struct use_list { typedef std::list<T> type; };
template<template<class> class container_policy> class A {
typedef typename container_policy<int>::type container;
...
container data;
};
int main() {
A<use_vector> a_v;
A<use_list> a_l;
}

class A{
public:
A(){};
~A(){};
void push_back(int i){ _vector_int_inst.push_back(i);}
void show(){
copy(_vector_int_inst.begin(), _vector_int_inst.end(),
ostream_iterator<int>(s
td::cout, "\n"));
}
private:
__Tp<int> _vector_int_inst;//__Tp _vector_int_inst;
};

int main(int argc, char *argv[])
{
A<std::vector> a;//A<std::vector<int> > a;
a.push_back(1);
a.push_back(2);
a.show();
}

V
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER