命名空间
变体
操作

std::experimental::optional<T>::operator->, std::experimental::optional<T>::operator*

来自 cppreference.cn
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行扩展 (parallelism TS)
并行扩展 2 (parallelism TS v2)
并发扩展 (concurrency TS)
并发扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
 
constexpr const T* operator->() const;
(1) (库基础 TS)
constexpr T* operator->();
(1) (库基础 TS)
constexpr const T& operator*() const&;
(2) (库基础 TS)
constexpr T& operator*() &;
(2) (库基础 TS)
constexpr const T&& operator*() const&&;
(2) (库基础 TS)
constexpr T&& operator*() &&;
(2) (库基础 TS)

访问包含的值。

1) 返回指向包含值的指针。
2) 返回对包含值的引用。

如果 *this 不包含值,则行为未定义。

目录

[编辑] 参数

(无)

[编辑] 返回值

指向或引用包含值的指针。

[编辑] 异常

不抛出异常。

[编辑] 注解

此运算符不检查 optional 是否包含值。如果需要检查访问,可以使用 value()value_or()

[编辑] 示例

#include <experimental/optional>
#include <iostream>
#include <string>
using namespace std::literals;
 
int main()
{
    std::experimental::optional<int> opt1 = 1;
    std::cout << *opt1 << '\n';
 
    std::experimental::optional<std::string> opt2 = "abc"s;
    std::cout << opt2->size() << '\n';
}

输出

1
3

[编辑] 参见

返回包含的值
(公共成员函数) [编辑]
如果可用,则返回包含的值,否则返回另一个值
(公共成员函数) [编辑]