Below is a header file & test driver. I compiled it ok in g++. But it failed in VC++7.0. How can I work around it in VC++7.0? Thank you so much !!!
#ifndef __TRAIT_H__
#define __TRAIT_H__
#include <string>
#include <iostream>
using namespace std;
template <class T, class A> struct serialize_trait {
typedef serialize_trait<T, A> self;
typedef A allocator_type;
typedef T value_type;
typedef self serializer_type;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::offset_type offset_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type *pointer;
allocator_type &alloc;
serialize_trait(allocator_type &allocator) : alloc(allocator) {}
void serialize(const value_type &x, offset_type o) {
memcpy(alloc.off2ptr(o),(void*)&x,size());
}
reference deserialize(offset_type o) {
return *(value_type*)alloc.off2ptr(o);
}
const_reference deserialize(offset_type o) const {
return *(value_type*)alloc.off2ptr(o);
}
void destruct(offset_type o) {}
size_type size() const { return sizeof(value_type); }
};
template <class Tr>
class object_wrapper: public Tr::value_type {
protected:
typedef object_wrapper<Tr> self;
typedef typename Tr::allocator_type allocator_type;
typedef Tr serializer_type;
typedef typename Tr::value_type value_type;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::offset_type offset_type;
serializer_type *serialize;
offset_type tp_off;
public:
object_wrapper<Tr> (const value_type &x, serializer_type *serializer, offset_type xp_off)
: value_type(x), serialize(serializer), tp_off(xp_off) {}
~object_wrapper() {
serialize->destruct(tp_off);
serialize->serialize(*(value_type*)this,tp_off);
};
object_wrapper<Tr> &operator=(const value_type &y) {
*static_cast<value_type*>(this)=y;
return *this;
};
};
template <class A> struct serialize_trait<std::string, A> {
typedef serialize_trait<std::string, A> self;
typedef A allocator_type;
typedef self serializer_type;
typedef std::string value_type;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::offset_type offset_type;
typedef object_wrapper<self> reference;
typedef value_type const_reference;
typedef object_wrapper<self> *pointer;
allocator_type &alloc;
serialize_trait(allocator_type &allocator) : alloc(allocator) {}
void serialize(const string &x, offset_type o) {
size_type n=x.length()+1;
offset_type to=alloc.nvalloc(n);
memcpy(alloc.off2ptr(to),x.c_str(),n);
*(offset_type*)alloc.off2ptr(o)=to;
}
reference deserialize(offset_type o) {
string s((char*)alloc.off2ptr(*(offset_type*)alloc.off2pt r(o)));
return reference(s,this,o);
}
const_reference deserialize(offset_type o) const {
return string((char*)alloc.off2ptr(*(offset_type*)alloc.o ff2ptr(o)));
}
void destruct(offset_type o) {
offset_type to=*(offset_type*)alloc.off2ptr(o);
alloc.free(to);
}
size_type size() const { return sizeof(offset_type); }
};
#endif
///////////////////////////////
#include <iostream>
#include "trait.h"
using namespace std;
int main() {
}