Namespaces
Variants

std::experimental:: propagate_const

From cppreference.net
ヘッダーで定義 <experimental/propagate_const>
template < class T >
class propagate_const ;
(ライブラリ基盤 TS v2)

std::experimental::propagate_const は、ポインタおよびポインタ風オブジェクトのためのconst伝播ラッパーです。constアクセスパスを通じてアクセスされる場合、ラップされたポインタを const へのポインタとして扱います。これが名前の由来です。

このクラスは、基となるポインタ風の型が対応する要件を満たす場合、 MoveConstructible および MoveAssignable の要件を満たしますが、 propagate_const CopyConstructible でも CopyAssignable でもありません。

型要件
-
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 と等価である

さらに、 T および const 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 member function)
(destructor)
(implicitly declared)
propagate_const を破棄し、含まれるポインタを破棄する
(public member function)
propagate_const オブジェクトを代入する
(public member function)
ラップされたポインタを交換する
(public member function)
オブザーバー
ラップされたポインタが指すオブジェクトへのポインタを返す
(public member function)
ラップされたポインタがnullかどうかをチェックする
(public member function)
ラップされたポインタを間接参照する
(public member function)
ポインタへの暗黙的変換関数
(public member function)

非メンバー関数

別の 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++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 3136 LFTSv2 意味のない T 型( int * const void * または const PtrLike など)が許可されていた 許可しない