473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jul 23 '05 #1
6 1917
Pe*******@gmail.com 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.

Jul 23 '05 #2
Pe*******@gmail.com 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
Jul 23 '05 #3
Can I use an identifier beginning with one underscore?

Best wishes,
Peng

Jul 23 '05 #4
Pe*******@gmail.com wrote:
Can I use an identifier beginning with one underscore?


Why?
Jul 23 '05 #5
I just want to whether it is legal or not. Because I already have some
code which have identifiers starting with undersore.

Peng

Jul 23 '05 #6
Pe*******@gmail.com 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
Jul 23 '05 #7

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

Similar topics

2
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program...
6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
2
by: Steven T. Hatton | last post by:
While thunbing through _C++ Templates, The Complete Guide_ (reckon I aught to read it?) I came across a discussion of using templates to "unroll" loops. I thought that looked like a good idea, so...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
1
by: Agoston Bejo | last post by:
Platform: VC++ 7.1 Hello, suppose I've got a template constructor in a class, and I would like to explicitly specify one of its parameters. What is the syntax for that? Or is there no such...
13
by: jsnX | last post by:
say i have a function object silly that takes a const ref to clowns class silly : public std::unary_function<const clown&, bool> { ... } and then i decide to feed it a bunch of pointers to...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
3
by: PengYu.UT | last post by:
I have the following two program. The first one compiles well, but the second one doesn't. The only difference between them is one more template parameter is added for the second program. Would...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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...

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.