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

C++17 std::any变量赋值后显示‘not empty (Small)’无法访问实际值

解决std::any栈在VS2019中无法查看/访问值的问题

首先明确:VS调试器里显示std::any为“not empty (Small)”是正常现象——这表示该std::any对象存储的是小类型对象(比如你的代码里的字符串字面量"char"或double值1.0),这类对象会直接存在std::any的内部缓冲区,不需要堆分配。但你看不到具体值,是因为调试器不会自动解析std::any的内部存储,且你的代码目前也没有正确存储实际输入值、没有用正确方式访问std::any内容。

你的代码存在的核心问题

  1. token处理逻辑错误:当前代码只是把所有非数字开头的token统一存成字符串"char",数字开头的统一存成1.0,完全没有存储输入的实际运算符、函数名或数值,这根本无法实现RPN求解的基础逻辑。
  2. 未正确访问std::any内容:要获取std::any存储的值,必须使用std::any_cast<T>模板,指定对应的类型(比如double或std::string),否则无法直接读取。

修正方案

1. 正确处理输入token

先区分不同类型的token,存储实际值:

  • 数值类型:将解析后的double存入std::any
  • 运算符/函数/括号:将对应的字符串存入std::any

2. 正确访问std::any存储的值

使用std::any_cast提取值,同时建议用std::any::type()判断存储的类型,避免类型不匹配导致异常。

修正后的代码示例

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <any>
#include <cmath>
#include <stdexcept>

using namespace std;

int main() 
{
    stack<any> equation;
    string input;
    getline(cin, input);

    int inputLength = input.length();

    // 预处理输入,添加必要空格和隐式乘号
    for (int i = 0; i < inputLength; i++) {
        if (input[i] == '(' && i + 1 < inputLength && input[i + 1] != ' ') {
            input.insert(input.begin() + i + 1, ' ');
            inputLength++;
        }
        else if (input[i] == ')' && i - 1 >= 0 && input[i - 1] != ' ') {
            input.insert(input.begin() + i, ' ');
            inputLength++;
        }
        else if (i < inputLength - 2 && input[i] == ')' && 
                 (input[i + 2] == '(' || (input[i + 2] >= '0' && input[i + 2] <= '9'))) {
            input.insert(input.begin() + i + 2, '*');
            input.insert(input.begin() + i + 3, ' ');
            inputLength += 2;
        }
    }

    stringstream ss(input);
    string token;
    while (ss >> token) {
        // 判断是否为数值(整数或double)
        bool isNumber = false;
        try {
            double num = stod(token);
            equation.push(num);
            isNumber = true;
        } catch (...) {}

        if (!isNumber) {
            // 运算符、函数、括号直接存字符串
            equation.push(token);
        }
    }

    // 测试访问栈中元素(示例)
    cout << "栈中元素(从顶到底):" << endl;
    stack<any> tempStack = equation;
    while (!tempStack.empty()) {
        any val = tempStack.top();
        tempStack.pop();
        if (val.type() == typeid(double)) {
            cout << "数值:" << any_cast<double>(val) << endl;
        } else if (val.type() == typeid(string)) {
            cout << "符号/函数:" << any_cast<string>(val) << endl;
        } else {
            cout << "未知类型" << endl;
        }
    }

    return 0;
}

调试时查看std::any值的方法

在VS2019调试器中,若要查看某个std::any对象的具体值:

  1. 打开监视窗口
  2. 添加监视项,比如针对栈顶元素:std::any_cast<double>(equation.top())(如果确定是数值类型),或者std::any_cast<string>(equation.top())(如果是字符串类型)
  3. 也可以通过val.type().name()查看存储的类型名称,辅助判断

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 22:20:33