flushing an archive outside the destructor

21 views
Skip to first unread message

Alessio M

unread,
Sep 8, 2020, 2:46:47 PM9/8/20
to cereal serialization library
Hi All

How does one flush an archive before destroying it? It doesn't seem possible. It seems like a very simple thing to add to cereal::OutputArchiveBase, but I wonder why archives were designed this way, and if I'm not doing something fundamentally wrong here.

I have the following setup

class BufferWhoseSyncCanThrow : public std::stringbuf
{
 protected:
    int sync() override { do_something_that_can_throw();  return 0; }
}

void serialize_my(Stuff const& stuff) {
    std::ostream stream(new  BufferWhoseSyncCanThrow);
    cereal::JSONOutputArchive json(stream);
    json(stuff);
    // json.flush(); // I'd like to be able to do this
}

If sync() throws, the C++ runtime will call terminate(). I know that I ought to catch and return -1 instead, but I actually don't really want to lose the exception context there.

What am I thinking wrong here?

Thank you in advance for any advice on this!
Reply all
Reply to author
Forward
0 new messages