名前空間
変種
操作

std::add_cv, std::add_const, std::add_volatile

提供: cppreference.com
< cpp‎ | types
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
型サポート
型の性質
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++20未満)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
定数評価文脈
サポートされている操作
関係と性質の問い合わせ
型変更
add_cvadd_constadd_volatile
(C++11)(C++11)(C++11)
型変換
(C++11)
(C++11)
(C++17)
(C++11)(C++20未満)(C++17)
 
ヘッダ <type_traits> で定義
template< class T >
struct add_cv;
(1) (C++11以上)
template< class T >
struct add_const;
(2) (C++11以上)
template< class T >
struct add_volatile;
(3) (C++11以上)

追加された cv 修飾を持つ (T が関数、参照、またはその cv 修飾をすでに持つ場合を除く) ことを除いて T と同じ型であるメンバ型 type が提供されます。

1) constvolatile を両方追加します。

2) const を追加します。

3) volatile を追加します。

目次

[編集] メンバ型

名前 定義
type cv 修飾が追加された型 T

[編集] ヘルパー型

template< class T >
using add_cv_t       = typename add_cv<T>::type;
(C++14以上)
template< class T >
using add_const_t    = typename add_const<T>::type;
(C++14以上)
template< class T >
using add_volatile_t = typename add_volatile<T>::type;
(C++14以上)

[編集] 実装例

template< class T >
struct add_cv { typedef const volatile T type; };
 
template< class T> struct add_const { typedef const T type; };
 
template< class T> struct add_volatile { typedef volatile T type; };

[編集]

#include <iostream>
#include <type_traits>
 
struct foo
{
    void m() { std::cout << "Non-cv\n"; }
    void m() const { std::cout << "Const\n"; }
    void m() volatile { std::cout << "Volatile\n"; }
    void m() const volatile { std::cout << "Const-volatile\n"; }
};
 
int main()
{
    foo{}.m();
    std::add_const<foo>::type{}.m();
    std::add_volatile<foo>::type{}.m();
    std::add_cv<foo>::type{}.m();
}

出力:

Non-cv
Const
Volatile
Const-volatile

[編集] 関連項目

(C++11)
型が const 修飾されているかどうか調べます
(クラステンプレート) [edit]
型が volatile 修飾されているかどうか調べます
(クラステンプレート) [edit]
指定された型から const, volatile またはその両方の指定子を削除します
(クラステンプレート) [edit]
(C++17)
引数への const な参照を取得します
(関数テンプレート) [edit]