How to specify a template as the typename of a template

P

PengYu.UT

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();
}
 
R

red floyd

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 said:
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.
 
V

Victor Bazarov

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;
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
 
P

PengYu.UT

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

Peng
 
V

Victor Bazarov

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
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top