命名空间
变体
操作

std::any::has_value

来自 cppreference.cn
< cpp‎ | utility‎ | any
 
 
 
 
bool has_value() const noexcept;
(自 C++17 起)

检查对象是否含值。

内容

[编辑] 参数

(无)

[编辑] 返回值

true 当且仅当实例含值时为 true

[编辑] 示例

#include <any>
#include <cassert>
#include <string>
 
int main()
{
    std::any a0;
    assert(a0.has_value() == false);
 
    std::any a1 = 42;
    assert(a1.has_value() == true);
    assert(std::any_cast<int>(a1) == 42);
    a1.reset();
    assert(a1.has_value() == false);
 
    auto a2 = std::make_any<std::string>("Andromeda");
    assert(a2.has_value() == true);
    assert(std::any_cast<std::string&>(a2) == "Andromeda");
    a2.reset();
    assert(a2.has_value() == false);
}

[编辑] 参见

销毁所含对象
(公开成员函数) [编辑]