【问题标题】:Failed values using boost::get使用 boost::get 的失败值
【发布时间】:2020-08-16 11:11:02
【问题描述】:

我想要做的只是理解这个异常的一般含义,所以我不想在上面附加任何代码。这是完整的错误: libc++abi.dylib: terminating with uncaught exception of type boost::wrapexcept<boost::bad_get>: boost::bad_get: failed value get using boost::get

【问题讨论】:

    标签: c++ boost c++14


    【解决方案1】:

    正如异常所说,boost::get 失败了。最可能的原因是您尝试从 get 中获取值的 boost::variant 当前不包含您尝试 get 的类型。例如,以下内容将抛出您看到的异常,因为 foo 当前持有 int,而不是 double

    int main (){
        boost::variant<int, double> foo;
        foo = 42;
        std::cout << boost::get<double>(foo);
    }
    

    Live Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2020-03-25
      相关资源
      最近更新 更多