跳转至

ev-drogon-any-attributes-001

字段 值
项目 drogonframework/drogon
固定 commit 3aae769cc405
路径 lib/inc/drogon/Attribute.h
行段 L61-L75
特性 std::any
许可证 MIT
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-10-25
测试代码 否
发现方式 tree-sitter

用法

用 std::any 作为请求级键值容器的值类型

为什么典型

HTTP 请求属性由框架与业务代码共同写入,类型在编译期无法统一;map 保存任意可复制值,并在读取时由调用方恢复具体类型。

摘录

    std::any &operator[](const std::string &key)
    {
        return attributesMap_[key];
    }

    /**
     * @brief Insert a key-value pair
     * @note here the any object can be created implicitly. for example
     * @code
       attributesPtr->insert("user name", userNameString);
       @endcode
     */
    void insert(const std::string &key, const std::any &obj)
    {
        attributesMap_[key] = obj;

备注

生产路径;注释明确说明 any 可由普通值隐式构造,后续读取需要显式 any_cast。

查看固定 commit 源码 · 返回证据索引