Re: partial template specialization
On 10 Okt., 13:01, Tomcat <tomek.kaczyn...@gmail.com> wrote:
Can you help me with a problem i have with a partial template
specialization, let's say i want to specialize template AB for a
container sdt::deque or any other. Is there any way to have all
methods and properties from 'general' AB and to specialize only
ONE method ? I think i coud do that using multiple inheritance,
is there any other way ?
This is only possible, if you fully specialize the member in
*all* template parameters (No unknown template parameters).
This does not work here, since T and Alloc are still "open")
el = buf.last();
I guess you meant:
el = buf.back();
template<class T,class Alloc
Should have probably written as
template<class T,class Alloc>
int _tmain(int argc, _TCHAR* argv[])
I read this as:
int main(int argc, char* argv[])
As usual, a little indirection helps here. The trick
is to partially specialize another class template:
#include <vector>
#include <deque>
#include <iostream>
using namespace std;
template<class T, template <class,class> class Cont,
typename Alloc>
struct ContHandler;
template<class T, template <class,class> class Cont,
typename Alloc = allocator<T> >
class AB{
template<class TT, template <class,class> class CC,
typename AA>
friend struct ::ContHandler;
public:
typedef Cont<T,Alloc> tCont;
tCont buf;
void write(T & el){
ContHandler<T, Cont, Alloc>::do_write(*this,
el);
}
void read(T & el){
el = buf.back();
}
};
template<class T, template <class,class> class Cont,
typename Alloc>
struct ContHandler {
static void do_write(AB<T, Cont, Alloc>& that,
T& el) {
cout << "general" << endl;
that.buf.push_back(el);
}
};
template<class T, typename Alloc>
struct ContHandler<T, std::deque, Alloc> {
static void do_write(AB<T, std::deque, Alloc>& that,
T& el) {
cout << "specialized" << endl;
std::deque<T,Alloc> buf;
that.buf.push_front(el);
}
};
class A{
public:
~A(){ cout<< "destructor" << endl;}
};
int main(int argc, char* argv[])
{
AB<A,std::vector> Buf;
A b;
Buf.write(b);
Buf.read(b);
AB<A,std::deque> Buf2;
A c;
Buf2.write(c);
Buf2.read(c);
}
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]