c++如何将std::any存储的数据序列化到文件

来源:编程学习作者:小雨头衔:草根站长
导读:本期聚焦于小伙伴创作的《c++如何将std::any存储的数据序列化到文件》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《c++如何将std::any存储的数据序列化到文件》有用,将其分享出去将是对创作者最好的鼓励。

std::any是C++17引入的通用类型容器,可以存储任意可拷贝的类型数据,但标准库没有提供它的序列化支持,因为std::any本身不记录类型的具体结构信息,直接序列化会出现数据类型丢失的问题。要实现将std::any存储的数据序列化到文件,需要自己设计类型映射和数据编码逻辑。

c++如何将std::any存储的数据序列化到文件

核心实现思路

序列化的核心是解决两个问题:记录当前std::any存储的数据类型,以及将对应类型的数据转换为可写入文件的字节流。我们可以设计一个类型注册机制,给每个需要支持序列化的类型分配唯一的类型标识,序列化时先写入类型标识,再写入数据内容,反序列化时根据类型标识还原数据。

类型注册与标识映射

首先定义一个类型标识的枚举或者字符串映射,这里用模板特化给每个类型生成唯一ID:

#include <any>
#include <unordered_map>
#include <string>
#include <fstream>
#include <iostream>
#include <vector>

// 基础类型ID生成模板
template<typename T>
struct TypeId {
    static int id;
};

// 初始化静态成员
template<typename T>
int TypeId<T>::id = -1;

// 类型注册器,给每个类型分配唯一ID
class TypeRegistry {
private:
    static int nextId;
    static std::unordered_map<std::string, int> typeNameToId;
    static std::unordered_map<int, std::string> idToTypeName;
public:
    template<typename T>
    static int registerType(const std::string& typeName) {
        if (typeNameToId.find(typeName) != typeNameToId.end()) {
            return typeNameToId[typeName];
        }
        int id = nextId++;
        typeNameToId[typeName] = id;
        idToTypeName[id] = typeName;
        TypeId<T>::id = id;
        return id;
    }

    static int getTypeId(const std::string& typeName) {
        auto it = typeNameToId.find(typeName);
        if (it != typeNameToId.end()) {
            return it->second;
        }
        return -1;
    }

    static std::string getTypeName(int id) {
        auto it = idToTypeName.find(id);
        if (it != idToTypeName.end()) {
            return it->second;
        }
        return "";
    }
};

// 静态成员初始化
int TypeRegistry::nextId = 0;
std::unordered_map<std::string, int> TypeRegistry::typeNameToId;
std::unordered_map<int, std::string> TypeRegistry::idToTypeName;

// 预注册常用类型
#define REGISTER_TYPE(T) template<> int TypeId<T>::id = TypeRegistry::registerType<T>(#T)
REGISTER_TYPE(int);
REGISTER_TYPE(double);
REGISTER_TYPE(std::string);
REGISTER_TYPE(std::vector<int>);

序列化接口设计

我们需要为每种支持的类型实现序列化和反序列化函数,然后通过std::any的类型匹配调用对应函数。这里用重载的方式实现不同类的序列化逻辑:

// 序列化int类型
void serialize(std::ofstream& ofs, int val) {
    ofs.write(reinterpret_cast<const char*>(&val), sizeof(val));
}

// 反序列化int类型
void deserialize(std::ifstream& ifs, int& val) {
    ifs.read(reinterpret_cast<char*>(&val), sizeof(val));
}

// 序列化double类型
void serialize(std::ofstream& ofs, double val) {
    ofs.write(reinterpret_cast<const char*>(&val), sizeof(val));
}

// 反序列化double类型
void deserialize(std::ifstream& ifs, double& val) {
    ifs.read(reinterpret_cast<char*>(&val), sizeof(val));
}

// 序列化string类型
void serialize(std::ofstream& ofs, const std::string& val) {
    size_t len = val.size();
    ofs.write(reinterpret_cast<const char*>(&len), sizeof(len));
    ofs.write(val.data(), len);
}

// 反序列化string类型
void deserialize(std::ifstream& ifs, std::string& val) {
    size_t len;
    ifs.read(reinterpret_cast<char*>(&len), sizeof(len));
    val.resize(len);
    ifs.read(&val[0], len);
}

// 序列化vector<int>类型
void serialize(std::ofstream& ofs, const std::vector<int>& val) {
    size_t len = val.size();
    ofs.write(reinterpret_cast<const char*>(&len), sizeof(len));
    for (int item : val) {
        ofs.write(reinterpret_cast<const char*>(&item), sizeof(item));
    }
}

// 反序列化vector<int>类型
void deserialize(std::ifstream& ifs, std::vector<int>& val) {
    size_t len;
    ifs.read(reinterpret_cast<char*>(&len), sizeof(len));
    val.resize(len);
    for (int i = 0; i < len; ++i) {
        ifs.read(reinterpret_cast<char*>(&val[i]), sizeof(val[i]));
    }
}

std::any的序列化与反序列化实现

接下来封装std::any的序列化函数,先判断std::any存储的类型,写入类型ID,再调用对应类型的序列化函数:

// 将std::any序列化到文件
bool serializeAny(std::ofstream& ofs, const std::any& data) {
    if (!data.has_value()) {
        // 空数据写入-1类型标识
        int emptyId = -1;
        ofs.write(reinterpret_cast<const char*>(&emptyId), sizeof(emptyId));
        return true;
    }
    // 判断类型并写入类型ID
    if (data.type() == typeid(int)) {
        int typeId = TypeId<int>::id;
        ofs.write(reinterpret_cast<const char*>(&typeId), sizeof(typeId));
        serialize(ofs, std::any_cast<int>(data));
    } else if (data.type() == typeid(double)) {
        int typeId = TypeId<double>::id;
        ofs.write(reinterpret_cast<const char*>(&typeId), sizeof(typeId));
        serialize(ofs, std::any_cast<double>(data));
    } else if (data.type() == typeid(std::string)) {
        int typeId = TypeId<std::string>::id;
        ofs.write(reinterpret_cast<const char*>(&typeId), sizeof(typeId));
        serialize(ofs, std::any_cast<std::string>(data));
    } else if (data.type() == typeid(std::vector<int>)) {
        int typeId = TypeId<std::vector<int>>::id;
        ofs.write(reinterpret_cast<const char*>(&typeId), sizeof(typeId));
        serialize(ofs, std::any_cast<std::vector<int>>(data));
    } else {
        std::cerr << "不支持的类型,无法序列化" << std::endl;
        return false;
    }
    return true;
}

// 从文件反序列化为std::any
std::any deserializeAny(std::ifstream& ifs) {
    int typeId;
    ifs.read(reinterpret_cast<char*>(&typeId), sizeof(typeId));
    if (typeId == -1) {
        return std::any();
    }
    std::string typeName = TypeRegistry::getTypeName(typeId);
    if (typeName == "int") {
        int val;
        deserialize(ifs, val);
        return val;
    } else if (typeName == "double") {
        double val;
        deserialize(ifs, val);
        return val;
    } else if (typeName == "std::string") {
        std::string val;
        deserialize(ifs, val);
        return val;
    } else if (typeName == "std::vector<int>") {
        std::vector<int> val;
        deserialize(ifs, val);
        return val;
    } else {
        std::cerr << "未知类型,无法反序列化" << std::endl;
        return std::any();
    }
}

完整使用示例

下面是完整的测试代码,演示如何存储不同类型的std::any到文件并读取:

int main() {
    // 准备要序列化的数据
    std::vector<std::any> datas;
    datas.push_back(100);
    datas.push_back(3.1415);
    datas.push_back(std::string("hello std::any"));
    datas.push_back(std::vector<int>{1,2,3,4,5});
    datas.push_back(std::any()); // 空数据

    // 序列化到文件
    std::ofstream ofs("data.bin", std::ios::binary);
    if (!ofs.is_open()) {
        std::cerr << "打开文件失败" << std::endl;
        return 1;
    }
    for (const auto& data : datas) {
        if (!serializeAny(ofs, data)) {
            std::cerr << "序列化失败" << std::endl;
            return 1;
        }
    }
    ofs.close();

    // 从文件反序列化
    std::ifstream ifs("data.bin", std::ios::binary);
    if (!ifs.is_open()) {
        std::cerr << "打开文件失败" << std::endl;
        return 1;
    }
    while (ifs.peek() != EOF) {
        std::any data = deserializeAny(ifs);
        if (data.has_value()) {
            if (data.type() == typeid(int)) {
                std::cout << "int: " << std::any_cast<int>(data) << std::endl;
            } else if (data.type() == typeid(double)) {
                std::cout << "double: " << std::any_cast<double>(data) << std::endl;
            } else if (data.type() == typeid(std::string)) {
                std::cout << "string: " << std::any_cast<std::string>(data) << std::endl;
            } else if (data.type() == typeid(std::vector<int>)) {
                std::vector<int> vec = std::any_cast<std::vector<int>>(data);
                std::cout << "vector<int>: ";
                for (int item : vec) {
                    std::cout << item << " ";
                }
                std::cout << std::endl;
            }
        } else {
            std::cout << "empty data" << std::endl;
        }
    }
    ifs.close();
    return 0;
}

注意事项

  • 新增支持的类型时,需要先通过REGISTER_TYPE宏注册类型,再实现对应的序列化和反序列化函数,最后在serializeAny和deserializeAny中添加类型判断分支。
  • 序列化自定义类型时,需要确保类型的所有成员变量都是可序列化的,并且序列化和反序列化的顺序完全一致。
  • 二进制序列化需要注意不同平台的字节序问题,如果需要跨平台使用,需要添加字节序转换逻辑。
  • std::any存储的类型必须是可拷贝的,否则无法正确序列化数据。

std::any序列化反序列化文件读写C++修改时间:2026-07-01 01:19:03

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。