473,569 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template specialization problem

i have a template as shown
template<typena me Sclass Indexer{};
i want to have a specialization for std::vector both const & non const
version.
template<typena me T,typename Aclass Indexer<std::ve ctor<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 std::vector<T,A const & non
const other for std::deque<T,Ac onst & non const.

thanks
abir
Jul 8 '08 #1
6 2592
abir wrote:
i have a template as shown
template<typena me Sclass Indexer{};
i want to have a specialization for std::vector both const & non const
version.
template<typena me T,typename Aclass Indexer<std::ve ctor<T,A {}
matches only with nonconst version. anyway to match it for both? and
get if it is const or nonconst?
Const and non-const variation of what? Of a vector? How would you use
it? Give an example that would cause an instantiation of your template.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 8 '08 #2
On Jul 8, 5:14 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
abir wrote:
i have a template as shown
template<typena me Sclass Indexer{};
i want to have a specialization for std::vector both const & non const
version.
template<typena me T,typename Aclass Indexer<std::ve ctor<T,A {}
matches only with nonconst version. anyway to match it for both? and
get if it is const or nonconst?

Const and non-const variation of what? Of a vector? How would you use
it? Give an example that would cause an instantiation of your template.
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
What i want is to have multiple pattern matching for same
specialization.
I have
template<typena me Sclass Indexer{};
i have specialization
template<typena me T,typename Aclass Indexer< std::vector<T,A {};
and
template<typena me T,typename Aclass Indexer < const std::vector<T,A >
{};
as both the specialization are nearly same, i don't want code
duplication, so i want a single specialization to match both, so that
the client can write,

Indexer<std::ve ctor<int idx;
or Indexer<const std::vector<int idx;
and both to match same specialization, and not anything else.

The alternative can be to write it without specialization and use
remove_const to allow nonconst -const conversion (like the way
iterators do),
but then i can have
Indexer<MyDucki dx; where MyDuck behaves like std::vector and causes
problem, which i don't want.

The third one is using a name like
VectorIndexer<s td::vector<int idx; or
VectorIndexer<t rue_typeidx; where it is directly coupled with vector
& template param only denotes constness rather than container type.
But then i have to have a name for each one, and (yet) can't alias
them to a common name.

Thanks.

abir
Jul 9 '08 #3
On Jul 8, 3:50 am, abir <abirba...@gmai l.comwrote:
i have a template as shown
template<typena me Sclass Indexer{};
i want to have a specialization for std::vector both const & non const
version.
template<typena me T,typename Aclass Indexer<std::ve ctor<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 std::vector<T,A const & non
const other for std::deque<T,Ac onst & non const.

thanks
abir
Would be nice to have a better idea of what you want.
std::vector< T, A is not a valid type.
Strange that you should require template specializations that
presumeably may attempt to modify a const container within. Indexer
and ConstantIndexer makes more sense to me (whatever Indexer might
be).

Uncomment the line in main, you may find the result to be rather
interesting.

#include <iostream>
#include <vector>
#include <deque>
#include <typeinfo>

template < typename T, typename A >
class Record
{
T t;
A a;
public:
Record(const T& t_, const A& a_) : t(t_), a(a_) { }
};

template< typename T, typename A,
typename V = std::vector< Record<T,A >
class Container
{
V v;
public:
void inspect() const { std::cout << typeid(v).name( ) << std::endl; }
void push_back(const Record<T,A>& rec) { v.push_back(rec ); }
};

int main()
{
Record< char, int r('a',00);

Container< char, int v;
v.inspect();
v.push_back(r); // ok

Container< char, int, std::deque< Record<char, int d;
d.inspect();

Container< char, int, const std::vector< Record<char, int cv;
cv.inspect();
// cv.push_back(r) ;
}
Jul 9 '08 #4
Salt_Peter wrote:
[..]
std::vector< T, A is not a valid type.
[..]
It isn't???
------------------------------ Perhaps this will explain:
#include <vector>
#include <iostream>
#include <ostream>

template<class Tclass Foo
{
public:
Foo() { std::cout << "Generic Foo template\n"; }
};

template<class T, class Aclass Foo<std::vector <T,A
{
public:
Foo() { std::cout << "Specialise d Foo template\n"; }
};

template<class UFoo<Ubar(U)
{
return Foo<U>();
}

int main()
{
bar(42);
std::vector<int vi;
bar(vi);
const std::vector<dou blevd;
bar(vd);
}
-------------------------------------------------

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 9 '08 #5
abir wrote:
On Jul 8, 5:14 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>abir wrote:
>>i have a template as shown
template<type name Sclass Indexer{};
i want to have a specialization for std::vector both const & non const
version.
template<type name T,typename Aclass Indexer<std::ve ctor<T,A {}
matches only with nonconst version. anyway to match it for both? and
get if it is const or nonconst?
Const and non-const variation of what? Of a vector? How would you use
it? Give an example that would cause an instantiation of your template.
>>[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

What i want is to have multiple pattern matching for same
specialization.
I have
template<typena me Sclass Indexer{};
i have specialization
template<typena me T,typename Aclass Indexer< std::vector<T,A {};
and
template<typena me T,typename Aclass Indexer < const std::vector<T,A >
>{};
Empty classes? I am guessing not, but my crystal ball is malfunctioning
at this time, I can't see into your mind to understand what their
differences or commonalities are.
as both the specialization are nearly same,
Actually as you wrote here, they *are* the same.
i don't want code
duplication, so i want a single specialization to match both, so that
the client can write,
[..]
(a) "Nearly the same" is not the same as "the same", is it? If there is
a lot in common between two types, extract the common part into the
third class and make it the base class for both.

(b) You didn't answer the question how your class is to be *used*.
Instantiation of a type is not its *use*, you need to show what
*behaviour* your type has to show what exactly you're after.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 9 '08 #6
On Jul 9, 5:32 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
abir wrote:
On Jul 8, 5:14 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
abir wrote:
i have a template as shown
template<typen ame Sclass Indexer{};
i want to have a specialization for std::vector both const & non const
version.
template<typen ame T,typename Aclass Indexer<std::ve ctor<T,A {}
matches only with nonconst version. anyway to match it for both? and
get if it is const or nonconst?
Const and non-const variation of what? Of a vector? How would you use
it? Give an example that would cause an instantiation of your template.
>[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
What i want is to have multiple pattern matching for same
specialization.
I have
template<typena me Sclass Indexer{};
i have specialization
template<typena me T,typename Aclass Indexer< std::vector<T,A {};
and
template<typena me T,typename Aclass Indexer < const std::vector<T,A >
{};

Empty classes? I am guessing not, but my crystal ball is malfunctioning
at this time, I can't see into your mind to understand what their
differences or commonalities are.
as both the specialization are nearly same,

Actually as you wrote here, they *are* the same.
i don't want code
duplication, so i want a single specialization to match both, so that
the client can write,
[..]

(a) "Nearly the same" is not the same as "the same", is it? If there is
a lot in common between two types, extract the common part into the
third class and make it the base class for both.

(b) You didn't answer the question how your class is to be *used*.
Instantiation of a type is not its *use*, you need to show what
*behaviour* your type has to show what exactly you're after.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I am not getting instant updates about my posts,due to some reason.
So the delay in replying, sorry for that.
The blank classes are ok, as i may have any implementation for them. I
only want to match them in template in some way. However as it is not
makes sense, so i am trying to elaborate on that.
I can only say what i want to write. As i don't know how to write the
one which i need, so it is little difficult to explain. I am trying my
best with code & example
I have a class template which looks like this (a lot of members are
not shown, however its behavior is more like an random access
iterator, but with some differences).
template<typena me S>
class VectorIndexer{
public:
typedef VectorIndexer<S self_type;
typedef VectorIndexer<t ypename
std::tr1::remov e_const<S>::typ enonconst_self;
friend class VectorIndexer<c onst S>;
public:
VectorIndexer(S & seq,typename S::size_type idx = 0) :
seq_(&seq),inde x_(idx){}
VectorIndexer(n onconst_self& other) :
seq_(other.seq_ ),index_(other. index_){}
private:
S* seq_;
typename S::size_type index_;
};

Now in client code i can have, (commented lines are not allowed,
hadn't shown assignment ops also)
typedef std::vector<int VI;
VI v;
v+=1,2,3,4,5,6, 7,8,9;
const VI& cv = v;
VectorIndexer<V Ii(v);
VectorIndexer<c onst VIi1(v);
//VectorIndexer<V Ii2(cv);
VectorIndexer<c onst VIi3(cv);
//VectorIndexer<V Ii4 = i1;
VectorIndexer<c onst VIi5 = i;
So far it is ok for me as i allowed const and non const matching and
allowed one way conversion.
However i hadn't disallowed other classes & containers so far.
Thus this works
typedef std::deque<intQ I;
QI q; q+=1,2,3,4,5;
VectorIndexer<Q Iqi(q);
However, i want a different implementation for deque based on class
name (not based on any trait). And if it is not specialized for that
class then
VectorIndexer<Q Iqi(q); should fail.

So the way i have it currently is a separate name DequeIndexer along
with separate implementation.
and access is
DequeIndexer<QI qi(q);
I don't want that.
I want (1)
Indexer<QIqi(q) ; Indexer<const QIcqi(q);
Indexer<VIvi(v) ; Indexer<const VIcvi(v); and so on where
implementation for const & non const are in same specialization as
shown above in VectorIndexer. I don't have any problem adding /
removing any additional template args, as long as i can call them in
this way.
Also i don't want any aux typedef like iterators which are tied up
with container (or from outside) like
indexer_types<Q I>::indexer qi(q);
indexer_types<Q I>::const_index er cqi(q);//or indexer_types<c onst
QI>::indexer cqi(q);
so what i want is to match all for those i specialized Indexer, eg
{QI ,const QI},{VI,const VI} and not for any other. And these pairs
should have same specialization as given in VectorIndexer.

I hope i made my aim & intension clearer. If any addition things are
necessary i will be able to provide.

and thanks for answering ...
Thanks
abir
Jul 10 '08 #7

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

Similar topics

17
6837
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because...
6
3339
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 like that possible? Some workaround? Thank you, Patrick
2
2527
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1...
8
7657
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
2
5767
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I...
5
1702
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something i havent correctly understood related to template specialization or is it some problem related to the compiler. Following is the code..
3
362
by: Scott Frazer | last post by:
I'm trying to do some template specialization and can't get it to work quite right. I have a templated base class: template <typename T> class SignalBase { ... }; and a derived class: template <typename T> class Signal : public SignalBase<T> {
2
2478
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode: VertexValueTyp& vertex(int i); that dereferences vertices internally in case VertexTyp is a pointer
5
1994
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ....
0
7703
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
7926
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. ...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
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...
0
3656
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
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
0
944
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.