命名空间
变体
操作

std::experimental::propagate_const

来自 cppreference.cn
< cpp‎ | 实验性
 
 
实验性
技术规范
文件系统库 (文件系统 TS)
库基础 (库基础 TS)
库基础 2 (库基础 TS v2)
库基础 3 (库基础 TS v3)
并行性扩展 (并行性 TS)
并行性扩展 2 (并行性 TS v2)
并发性扩展 (并发性 TS)
并发扩展 2 (并发 TS v2)
概念 (概念 TS)
范围 (范围 TS)
反射 (反射 TS)
数学特殊函数 (特殊函数 TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
 
template< class T >
class propagate_const;
(库基础 TS v2)

std::experimental::propagate_const 是一个对指针和类指针对象进行 const 传播的包装器。当通过 const 访问路径访问时,它将包装的指针视为指向 const 的指针,因此得名。

如果底层类指针类型满足相应要求,则该类满足 可移动构造可移动赋值 的要求,但 propagate_const 既不是 可复制构造 也不是 可复制赋值 的。

类型要求
-
T 必须是 cv-非限定指针到对象类型或 cv-非限定类指针类型,如下所述。

目录

[编辑] 类指针类型的要求

如果 T 是类类型,则它必须满足本小节中的要求。

给定

  • t,一个类型为 T 的可修改 左值表达式
  • ct,一个类型为 const T 的左值,表示与 t 相同的对象(等同于 C++17 起的 std::as_const(t)),
  • element_type,一个对象类型。

以下表达式必须有效并具有其指定的效果

表达式 返回类型 前置条件 操作语义
t.get() element_type*
ct.get() element_type*const element_type* t.get() == ct.get()
*t element_type& t.get() != nullptr *t 指向与 *(t.get()) 相同的对象
*ct element_type&const element_type& ct.get() != nullptr *ct 指向与 *(ct.get()) 相同的对象
t.operator->() element_type* t.get() != nullptr t.operator->() == t.get()
ct.operator->() element_type*const element_type* ct.get() != nullptr ct.operator->() == ct.get()
(bool)t bool (bool)t 等同于 t.get() != nullptr
(bool)ct bool (bool)ct 等同于 ct.get() != nullptr

此外,Tconst T 应可上下文转换为 bool

此外,如果 T 可隐式转换为 element_type*,则 (element_type*)t 应等于 t.get()。类似地,如果 const T 可隐式转换为 const element_type*,则 (const element_type*)ct 应等于 ct.get()

[编辑] 成员类型

成员类型 定义
element_type std::remove_reference_t<decltype(*std::declval<T&>())>T 指向的对象的类型

[编辑] 成员函数

构造一个新的 propagate_const
(public 成员函数) [编辑]
(析构函数)
(隐式声明)
销毁 propagate_const,销毁其中包含的指针
(public 成员函数) [编辑]
赋值 propagate_const 对象
(public 成员函数) [编辑]
交换被包装的指针
(public 成员函数) [编辑]
观察器
返回指向被包装指针所指向的对象的指针
(public 成员函数) [编辑]
检查被包装的指针是否为空
(public 成员函数) [编辑]
解引用被包装的指针
(public 成员函数) [编辑]
隐式转换为指针的函数
(public 成员函数) [编辑]

[编辑] 非成员函数

与另一个 propagate_const、另一个指针或 nullptr 进行比较
(函数模板) [编辑]
特化 swap 算法
(函数模板) [编辑]
检索对被包装的类指针对象的引用
(函数模板) [编辑]

[编辑] 辅助类

propagate_const 提供哈希支持
(类模板特化) [编辑]
propagate_const 提供的标准比较函数对象特化
(类模板特化) [编辑]

[编辑] 示例

#include <experimental/propagate_const>
#include <iostream>
#include <memory>
 
struct X
{
    void g() const { std::cout << "X::g (const)\n"; }
    void g() { std::cout << "X::g (non-const)\n"; }
};
 
struct Y
{
    Y() : m_propConstX(std::make_unique<X>()), m_autoPtrX(std::make_unique<X>()) {}
 
    void f() const
    {
        std::cout << "Y::f (const)\n";
        m_propConstX->g();
        m_autoPtrX->g();
    }
 
    void f()
    {
        std::cout << "Y::f (non-const)\n";
        m_propConstX->g();
        m_autoPtrX->g();
    }
 
    std::experimental::propagate_const<std::unique_ptr<X>> m_propConstX;
    std::unique_ptr<X> m_autoPtrX;
};
 
int main()
{
    Y y;
    y.f();
 
    const Y cy;
    cy.f();
}

输出

Y::f (non-const)
X::g (non-const)
X::g (non-const)
Y::f (const)
X::g (const)
X::g (non-const)

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 3136 LFTSv2 允许使用无意义的 T 类型,如 int* constvoid*const PtrLike 已禁用