首页 > 试题广场 >

以下代码关于std::any(C++17),程序的行为是什么

[单选题]
以下代码关于std::any(C++17),程序的行为是什么?
#include <any>
#include <iostream>
#include <string>
int main() {
    std::any a = 42;
    a = std::string("hello");
    try {
        auto v = std::any_cast<int>(a);
        std::cout << v;
    } catch (const std::bad_any_cast&) {
        std::cout << "bad_cast";
    }
}
  • 输出 hello
  • 输出 42
  • 输出 bad_cast
  • 编译错误
  • 未定义行为

这道题你会答吗?花几分钟告诉大家答案吧!