473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get template parameter, when i match a template?

I am matching a template, and specializing based of a template, rather
than a single class.

The codes are like,
template<templa te<typename T,typename Alloc = std::allocator< T>
>class C>
class pix{
};

template<>
class pix<vector>{
};
The matching works perfectly.
Now how will i get the template parameters in the specialized class?
I want to do things like,
template<>
class pix<std::vector >{
private:
std::vector<T>* v_;
};
From where will I get T & Alloc fro which it is a match?

Thanks
abir
Jun 30 '08 #1
4 2054
abir wrote:
I am matching a template, and specializing based of a template, rather
than a single class.

The codes are like,
template<templa te<typename T,typename Alloc = std::allocator< T>
>>class C>
class pix{
};

template<>
class pix<vector>{
};
The matching works perfectly.
Now how will i get the template parameters in the specialized class?
I want to do things like,
template<>
class pix<std::vector >{
private:
std::vector<T>* v_;
};
Why a pointer to a vector. That looks fishy.
From where will I get T & Alloc fro which it is a match?
You won't get them from anywhere. You have to supply them.

BTW: this is not related to the class being specialized. If you consider the
unspecialized pix, you will find that you cannot get at T and Alloc either
because they are just placeholders for something you still need to specify.
Best

Kai-Uwe Bux
Jun 30 '08 #2
On Jun 30, 4:52 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
abir wrote:
I am matching a template, and specializing based of a template, rather
than a single class.
The codes are like,
template<templa te<typename T,typename Alloc = std::allocator< T>
>class C>
class pix{
};
template<>
class pix<vector>{
};
The matching works perfectly.
Now how will i get the template parameters in the specialized class?
I want to do things like,
template<>
class pix<std::vector >{
private:
std::vector<T>* v_;
};

Why a pointer to a vector. That looks fishy.
From where will I get T & Alloc fro which it is a match?

You won't get them from anywhere. You have to supply them.

BTW: this is not related to the class being specialized. If you consider the
unspecialized pix, you will find that you cannot get at T and Alloc either
because they are just placeholders for something you still need to specify.

Best

Kai-Uwe Bux
It can be reference to a vector instead of pointer. I want to have an
external iterator kind of thing, which i name pix, which is portable,
i.e can be stored as long as the element at the location exists,
irrespective of whether container resizes or not. (Of course they can
be made immune to pop_front,push_ front, insert, erase etc also)
The pointer to a vector is not at all fishy in this case, almost all
(not all) iterators has a pointer to the container.
I want something like,
typedef vector<int V;
V v; v.push_back() a few elements ...
pix<Vp(v);
++p; *p; get element
v.push_back() a few more.
*p to get element.

so for a container C , i wanted pix as,
template<typena me C>
class pix{
C& c_;
C::size_type index_;
};
when C is a vector, but another implementation for another C. Only
problem is C itself is a template.

Why the template template parameters are "just placeholders" when the
instance is a full class?
Any other way to do it?

Thanks
abir
Jun 30 '08 #3
abir wrote:
On Jun 30, 4:52 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>abir wrote:
I am matching a template, and specializing based of a template, rather
than a single class.
The codes are like,
template<templa te<typename T,typename Alloc = std::allocator< T>
class C>
class pix{
};
template<>
class pix<vector>{
};
The matching works perfectly.
Now how will i get the template parameters in the specialized class?
I want to do things like,
template<>
class pix<std::vector >{
private:
std::vector<T>* v_;
};

Why a pointer to a vector. That looks fishy.
From where will I get T & Alloc fro which it is a match?

You won't get them from anywhere. You have to supply them.

BTW: this is not related to the class being specialized. If you consider
the unspecialized pix, you will find that you cannot get at T and Alloc
either because they are just placeholders for something you still need to
specify.

Best

Kai-Uwe Bux

It can be reference to a vector instead of pointer. I want to have an
external iterator kind of thing, which i name pix, which is portable,
i.e can be stored as long as the element at the location exists,
irrespective of whether container resizes or not. (Of course they can
be made immune to pop_front,push_ front, insert, erase etc also)
The pointer to a vector is not at all fishy in this case, almost all
(not all) iterators has a pointer to the container.
I want something like,
typedef vector<int V;
V v; v.push_back() a few elements ...
pix<Vp(v);
++p; *p; get element
v.push_back() a few more.
*p to get element.

so for a container C , i wanted pix as,
template<typena me C>
class pix{
C& c_;
C::size_type index_;
};
when C is a vector, but another implementation for another C. Only
problem is C itself is a template.
I see. In this case, I think, you want a pointer and not a reference. That
way, you won't run into trouble implementing an assignment operator for
pix.

Why the template template parameters are "just placeholders" when the
instance is a full class?
This is too vague. The instance of what is a class?

Anyway, when you have a template template parameter, what you pass as a
parameter is a template. In you case, you pass std::vector. Note that you
do not pass an instantiation of that template, i.e., you do not pass
something like std::vector<int >. You just pass std::vector. The pix class
would be able to use the parameter C to do stuff like C<int>.
Any other way to do it?
You don't want to use template template parameters. Use an ordinary typename
parameter:

template < typename Sequence >
class pix;

and then specialize for vectors:

template < typename T, typename A >
class pix< std::vector< T, A {
...
};

for deques:

template < typename T, typename A >
class pix< std::deque< T, A {
...
};

and for lists:

template < typename T, typename A >
class pix< std::list< T, A {
...
};

(not run by a compiler)
Best

Kai-Uwe Bux
Jun 30 '08 #4
On Jun 30, 5:31 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
abir wrote:
On Jun 30, 4:52 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
abir wrote:
I am matching a template, and specializing based of a template, rather
than a single class.
The codes are like,
template<templa te<typename T,typename Alloc = std::allocator< T>
class C>
class pix{
};
template<>
class pix<vector>{
};
The matching works perfectly.
Now how will i get the template parameters in the specialized class?
I want to do things like,
template<>
class pix<std::vector >{
private:
std::vector<T>* v_;
};
Why a pointer to a vector. That looks fishy.
From where will I get T & Alloc fro which it is a match?
You won't get them from anywhere. You have to supply them.
BTW: this is not related to the class being specialized. If you consider
the unspecialized pix, you will find that you cannot get at T and Alloc
either because they are just placeholders for something you still need to
specify.
Best
Kai-Uwe Bux
It can be reference to a vector instead of pointer. I want to have an
external iterator kind of thing, which i name pix, which is portable,
i.e can be stored as long as the element at the location exists,
irrespective of whether container resizes or not. (Of course they can
be made immune to pop_front,push_ front, insert, erase etc also)
The pointer to a vector is not at all fishy in this case, almost all
(not all) iterators has a pointer to the container.
I want something like,
typedef vector<int V;
V v; v.push_back() a few elements ...
pix<Vp(v);
++p; *p; get element
v.push_back() a few more.
*p to get element.
so for a container C , i wanted pix as,
template<typena me C>
class pix{
C& c_;
C::size_type index_;
};
when C is a vector, but another implementation for another C. Only
problem is C itself is a template.

I see. In this case, I think, you want a pointer and not a reference. That
way, you won't run into trouble implementing an assignment operator for
pix.
Yes, that is the only reason I had to use pointer instead of
reference.
Why the template template parameters are "just placeholders" when the
instance is a full class?

This is too vague. The instance of what is a class?

Anyway, when you have a template template parameter, what you pass as a
parameter is a template. In you case, you pass std::vector. Note that you
do not pass an instantiation of that template, i.e., you do not pass
something like std::vector<int >. You just pass std::vector. The pix class
would be able to use the parameter C to do stuff like C<int>.
Any other way to do it?

You don't want to use template template parameters. Use an ordinary typename
parameter:

template < typename Sequence >
class pix;

and then specialize for vectors:

template < typename T, typename A >
class pix< std::vector< T, A {
...
};

for deques:

template < typename T, typename A >
class pix< std::deque< T, A {
...
};

and for lists:

template < typename T, typename A >
class pix< std::list< T, A {
...
};

(not run by a compiler)
Got it. It runs perfectly & works according to my wishes!
Thanks a lot.
BTW, is there any already existing code which can refer to a slice of
sequence which doesn't get invalidated due to memory capacity related
issues? (I know blitz array boost numerics etc have slicing , but the
containers are non re sizable there ). I am very much in need for
that.
Best

Kai-Uwe Bux
abir
Jun 30 '08 #5

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

Similar topics

3
2200
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call ambigous because of // odering deduction of the function signature,
4
2425
by: Kevin Dean | last post by:
I'm trying to create an XSL transformation that will strip out development-specific attributes from deployment descriptors and other XML files. I have already successfully done so with web.xml but I'm at a complete loss as to what is wrong with the one below. This is a very abbreviated server-config.wsdd: <?xml version="1.0"...
7
2497
by: Rolf Kemper | last post by:
Dear All, somehow I remember that such or similar question was discussed already somewhere. But I can't find it anymore. I have a template calling itself. As long it goes deeper into the hierarchy (by the key) I can set the CurrentY parameter by itself + some constant correctly. Hence which each call the CurrentY gets bigger. But when...
3
2691
by: shaun roe | last post by:
I have a document about 4 levels deep and in my XSLT I want to generate a unique string ID for each basic element based on its path through the hierarchy. If I use recursion, I am continually accessing the root element ID, here is a typical call: <xsl:variable name="fullPath"...
6
2427
by: Kaba | last post by:
Here is a short snippet of code that does not compile (tested with Vc8 and Comeau). This is because somehow Vector<0gets instantiated, for which the array size goes to 0. However, I don't quite get it why it gets instantiated in the first place. So why does not this compile? template <int N> struct Vector { int data_; };
2
2571
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if I'm doing something wrong and g++ just doesn't notice, or if the people at Intel are doing something weird... What am I trying to do? I'm trying to...
2
2364
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector> class Ring {
6
2282
by: abir | last post by:
i have a template template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass Indexer<std::vector<T,A {} matches only with nonconst version. anyway to match it for both? and get if it is const or nonconst? Actually i want 2 specialization, one for...
8
287
by: Konstantin | last post by:
I have two possible implementations of my class: template <typename T> class MyContainer { public: typedef typename std::set<T>::const_iterator const_iterator; void add( T id ) { data.insert(id); } void remove( T id ) { data.erase(id); } private:
0
7478
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
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...
0
7668
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. ...
0
5984
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
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...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.