使用Cereal序列化时遇错误:Cereal不支持序列化原始指针
Hey there, let’s work through this Cereal serialization error you’re facing. I’ve dealt with this exact issue before when working with nested containers and mixed basic types, so I can break down what’s happening and how to fix it.
Why This Error Pops Up (Even With "Safe" Types)
You mentioned you’re using nested vectors (vector<vector<ushort>>), uint, and vector<string>—all types Cereal natively supports. So why the raw pointer error? Usually, it boils down to one of these sneaky issues:
- You’re accidentally passing a raw pointer to the Cereal archive instead of the object itself (e.g., using
&my_vector.data()instead of justmy_vector). - A hidden raw pointer exists in your code—maybe in a custom struct/class you’re serializing alongside these types, or a typo in your serialization logic that creates a pointer where you don’t want one.
- Cereal explicitly blocks raw pointer serialization because it can’t safely track ownership or avoid dangling pointers during deserialization.
Step-by-Step Fixes
Let’s go through the most common solutions:
Double-Check Your Serialization Calls
Make sure you’re passing objects (or references) to the archive, not pointers. For example:
❌ Wrong:ar & *my_nested_vec.data();(this dereferences a raw pointer to the vector’s underlying array)
✅ Correct:ar & my_nested_vec;(directly pass the vector object—Cereal handles nested vectors out of the box)Verify All Included Types Are Pointer-Free
If you’re serializing a custom type alongside your vectors/uints, check for any raw pointer members (likeushort*). If you find them:- Replace raw pointers with smart pointers (
std::unique_ptrorstd::shared_ptr). Cereal has full support for these—just include#include <cereal/types/memory.hpp>and serialize them like any other type.
- Replace raw pointers with smart pointers (
Ensure You’ve Got the Right Headers
Cereal requires specific headers for container types:- For vectors (including nested ones):
#include <cereal/types/vector.hpp> - For strings:
#include <cereal/types/string.hpp> - For smart pointers (if you switched to them):
#include <cereal/types/memory.hpp>
Don’t forget your archive header too (e.g.,#include <cereal/archives/binary.hpp>for binary serialization).
- For vectors (including nested ones):
Example Working Code
Here’s a quick snippet that correctly serializes all your mentioned types without triggering the pointer error:#include <cereal/archives/binary.hpp> #include <cereal/types/vector.hpp> #include <cereal/types/string.hpp> #include <fstream> #include <cstdint> int main() { // Sample data to serialize std::vector<std::vector<unsigned short>> nested_vec = {{10, 20, 30}, {40, 50}}; uint32_t my_uint = 98765; std::vector<std::string> str_list = {"cereal", "serialization", "ftw"}; // Serialize to file std::ofstream out_file("my_data.bin", std::ios::binary); cereal::BinaryOutputArchive out_archive(out_file); out_archive(nested_vec, my_uint, str_list); // Deserialize back std::vector<std::vector<unsigned short>> restored_nested; uint32_t restored_uint; std::vector<std::string> restored_strs; std::ifstream in_file("my_data.bin", std::ios::binary); cereal::BinaryInputArchive in_archive(in_file); in_archive(restored_nested, restored_uint, restored_strs); return 0; }
Quick Troubleshooting Tip
If you’re still stuck, comment out parts of your serialization code one by one. This will help you isolate exactly which line or type is triggering the raw pointer error—chances are it’s a small typo or overlooked pointer somewhere.
内容的提问来源于stack exchange,提问作者Sleepless

