Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 8th, 2008, 08:55 AM
abir
Guest
 
Posts: n/a
Default template specialization problem

i have a template as shown
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 std::vector<T,Aconst & non
const other for std::deque<T,Aconst & non const.

thanks
abir
  #2  
Old July 8th, 2008, 01:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: template specialization problem

abir wrote:
Quote:
i have a template as shown
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?
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.
Quote:
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  #3  
Old July 9th, 2008, 06:55 AM
abir
Guest
 
Posts: n/a
Default Re: template specialization problem

On Jul 8, 5:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
abir wrote:
Quote:
i have a template as shown
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?
>
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.
>
Quote:
[..]
>
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<typename Sclass Indexer{};
i have specialization
template<typename T,typename Aclass Indexer< std::vector<T,A {};
and
template<typename T,typename Aclass Indexer < const std::vector<T,A>
Quote:
{};
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::vector<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<MyDuckidx; where MyDuck behaves like std::vector and causes
problem, which i don't want.

The third one is using a name like
VectorIndexer<std::vector<int idx; or
VectorIndexer<true_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
  #4  
Old July 9th, 2008, 08:35 AM
Salt_Peter
Guest
 
Posts: n/a
Default Re: template specialization problem

On Jul 8, 3:50 am, abir <abirba...@gmail.comwrote:
Quote:
i have a template as shown
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 std::vector<T,Aconst & non
const other for std::deque<T,Aconst & 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);
}
  #5  
Old July 9th, 2008, 01:35 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: template specialization problem

Salt_Peter wrote:
Quote:
[..]
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 << "Specialised Foo template\n"; }
};

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

int main()
{
bar(42);
std::vector<intvi;
bar(vi);
const std::vector<doublevd;
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
  #6  
Old July 9th, 2008, 01:35 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: template specialization problem

abir wrote:
Quote:
On Jul 8, 5:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
>abir wrote:
Quote:
>>i have a template as shown
>>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?
>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.
>>
Quote:
>>[..]
>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<typename Sclass Indexer{};
i have specialization
template<typename T,typename Aclass Indexer< std::vector<T,A {};
and
template<typename T,typename Aclass Indexer < const std::vector<T,A>
Quote:
>{};
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.
Quote:
as both the specialization are nearly same,
Actually as you wrote here, they *are* the same.
Quote:
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
  #7  
Old July 10th, 2008, 06:05 AM
abir
Guest
 
Posts: n/a
Default Re: template specialization problem

On Jul 9, 5:32 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
abir wrote:
Quote:
On Jul 8, 5:14 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Quote:
abir wrote:
>i have a template as shown
>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?
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.
>
Quote:
Quote:
>[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
>
Quote:
What i want is to have multiple pattern matching for same
specialization.
I have
template<typename Sclass Indexer{};
i have specialization
template<typename T,typename Aclass Indexer< std::vector<T,A {};
and
template<typename T,typename Aclass Indexer < const std::vector<T,A>
Quote:
{};
>
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.
>
Quote:
as both the specialization are nearly same,
>
Actually as you wrote here, they *are* the same.
>
Quote:
i don't want code
>
Quote:
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<typename S>
class VectorIndexer{
public:
typedef VectorIndexer<Sself_type;
typedef VectorIndexer<typename
std::tr1::remove_const<S>::typenonconst_self;
friend class VectorIndexer<const S>;
public:
VectorIndexer(S& seq,typename S::size_type idx = 0) :
seq_(&seq),index_(idx){}
VectorIndexer(nonconst_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<intVI;
VI v;
v+=1,2,3,4,5,6,7,8,9;
const VI& cv = v;
VectorIndexer<VIi(v);
VectorIndexer<const VIi1(v);
//VectorIndexer<VIi2(cv);
VectorIndexer<const VIi3(cv);
//VectorIndexer<VIi4 = i1;
VectorIndexer<const 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<intQI;
QI q; q+=1,2,3,4,5;
VectorIndexer<QIqi(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<QIqi(q); should fail.

So the way i have it currently is a separate name DequeIndexer along
with separate implementation.
and access is
DequeIndexer<QIqi(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<QI>::indexer qi(q);
indexer_types<QI>::const_indexer cqi(q);//or indexer_types<const
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
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles