You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2019 C++17初始化静态结构体数组std::any成员数据异常问题

我按如下方式定义了一组静态对象:

static struct rpn_name_type {
    const std::string name;
    e_search_in search_in;
    funct_op& op;
    std::any obj;
    std::any val;

}rpn_names[] = {
//conditionals
    {"group_and",       es_null,        group_and_or,       std::any(condition_type({true})),       std::any(and_type()) },
    {"group_and_end",   es_null,        group_and_or_end,   std::any(condition_type({false})),      std::any(and_type()) },
    {"group_or",        es_null,        group_and_or,       std::any(condition_type({true})),       std::any(or_type()) },
    {"group_or_end",    es_null,        group_and_or_end,   std::any(condition_type({false})),      std::any(or_type()) },
//project data
    {"percent_funded_1",es_project,     percent_funded_1,   std::any(),         std::any(tup_type_char_ii{})},
    {"expend_year_1",   es_project,     expend_year_1,      std::any(),         std::any(tup_type_char_ii{})},
//databaes data
    {"remain",          es_records,     remain,             std::any(),         std::any(int_pair_type{})},
    {"replace",         es_records,     replace,            std::any(),         std::any(int_pair_type{})},
    {"description_has", es_records,     description_has,    std::any(),         std::any(std::wstring{})},
//the nulllllllll
    {"null",            es_null,        null,               std::any(),         std::any()},
};

此前我在运行时为es_project和es_records类别对象的val成员赋值,但需要逐一适配所有操作类型,逻辑十分繁琐;如果可以直接按类型条件初始化赋值,就能大幅简化代码。

但我遇到了异常问题:要么是我存在认知遗漏,要么是VS编译器存在bug——percent_funded_1和expend_year_1两个条目的val成员本应是tup_type_char_ii类型,实际却被识别为and_type,相关类型定义如下:

struct and_type {};
struct or_type {};

运行时截图

using int_pair_type = std::pair<int32_t, int32_t>;
using tup_type_char_ii = std::tuple<const char*, int, int>;

上述两个条目本应和其他数组元素一样正常初始化,却出现了上述类型错乱的异常。

更新

目前我在向容器添加操作时,通过如下代码做临时修复:

if (anop.name->search_in != es_null && anop.val.type() == typeid(and_type))
    anop.val = tup_type_char_ii{};

更新2

补充说明:我使用的是最新版VS2019,采用C17标准编译时会触发该错误,切换到最新C20标准后问题消失。

内容的提问来源于stack exchange,提问作者lakeweb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 22:18:32