Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ - data serialization

Name: Anonymous 2011-07-06 7:44

I needed to serialize some simple data structures in C++ code and remembered how to do it in C#. It was like 9001 lines of code of boilerplate crap in .NET environment.

Well, in C++ it was of course hard because C++ is such crappy language, rite?


template<typename T>
bool serializeData(const T &data, std::ostream &os)
{
    os.write(reinterpret_cast<const char*>(&data), sizeof(data));
    return os.good();
}

template<typename T>
bool deserializeData(T &data, std::istream &is)
{
    is.read(reinterpret_cast<char*>(&data), sizeof(data));
    return is.good();
}

Name: Anonymous 2011-07-06 20:03

>>17
And yes. You shouldn't buy into the solution of C++ being a high-level language like Java or C#. It's not. It's a low-level programming language with a variety of higher-level abstractions at your disposal.

In fact, I would say people who strive hard to eliminate all instances of void* in the name of "type safety" are following a practice that should be considered harmful. Type safety good, but it's just an illusion. And when you end up sacrificing performance in the name of "type safety," and the end result is code that is just as easy to break with a few pointer casts anyway, what really are you gained?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List