C++:最安全的施放提升方式::<type>可选类型

C++: Safest way to cast boost::optional<type> to type

本文关键字:type gt 类型 lt 方式 安全 施放提 C++      更新时间:2023-10-16

C++boost::可选问题。如何将boost::optional myInt强制转换为int(安全)。在我的程序中,我有一个if语句,用于检查myInt是否已初始化,因此为了可读性,我希望将其作为int而不是boost::可选函数在不同的函数中传递。提前感谢!

你不能只做吗

boost::optional<int> x;
//....
//....
if (x)
{
    int y = *x;  // or y = x.get();
}