serialization question (using composite STL classes)

B

Bit Byte

Suppose I have a data variable defined thus:

typedef std::vector<std::pair<std::string,std::string> > InfoVector ;

I have written a template function to generalize serialization:

template <class T> std::eek:stream& Serialize(std::eek:stream& out, const T&
value)
{
out.write(reinterpret_cast<const char*>(&value), sizeof(T)) ) ;
}

Can I pass InfoVector to my template function (or do I need a partial
specialized template ?). Any clarification on this issue will be very
helpful
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Bit said:
Suppose I have a data variable defined thus:
typedef std::vector<std::pair<std::string,std::string> > InfoVector ;

This is not the definition of a variable.
I have written a template function to generalize serialization:

template <class T> std::eek:stream& Serialize(std::eek:stream& out, const T&
value)
{
out.write(reinterpret_cast<const char*>(&value), sizeof(T)) ) ;
}

This is not serialization at all, is just raw data writing.

http://en.wikipedia.org/wiki/Serialization
 
B

bjeremy

Bit said:
Suppose I have a data variable defined thus:

typedef std::vector<std::pair<std::string,std::string> > InfoVector ;

I have written a template function to generalize serialization:

template <class T> std::eek:stream& Serialize(std::eek:stream& out, const T&
value)
{
out.write(reinterpret_cast<const char*>(&value), sizeof(T)) ) ;
}

Can I pass InfoVector to my template function (or do I need a partial
specialized template ?). Any clarification on this issue will be very
helpful

Your writing the address of your InfoVector (plus some garbage since
you are writing sizeof(InfoVector) bytes which is greater than and
address). As Julian points out, this is not serialization. You would
need to step through the vector and serialize the value (not reference)
of each pair respectively in such a way as a copy of the original
vector can be pieced back together again. Java has some langauge
support for this, I think Boost has some libraries for C++, however, I
never used them.
 
D

David Harmon

On Wed, 03 Jan 2007 21:51:13 +0000 in comp.lang.c++, Bit Byte
Can I pass InfoVector to my template function

Not a chance! std::vector, std::string, and many other classes manage
dynamic data that has no resemblance to anything compile-time sizeof
knows about.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Diego said:
But raw writing is useful when endianess is not an issue

Usefulness or lack or it are not reasons to call it serialization.
 
D

Diego Martins

Julián Albo said:
Usefulness or lack or it are not reasons to call it serialization.

you are right

but my point is, in most cases, one can use raw writing which is fast
and simple, instead of creating a bunch of boring serialization classes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top