Namespaces
Variants

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

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
add_cv add_const add_volatile
(C++11) (C++11) (C++11)
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <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以降)

type メンバ型を提供します。これは、 T が関数、参照、または既にこのCV修飾子を持っている場合を除き、 T にCV修飾子が追加されたものと同じです。

1) 両方の const volatile を追加する
2) 追加 const
3) 追加 volatile

プログラムがこのページで説明されているテンプレートのいずれかに対して特殊化を追加する場合、動作は未定義です。

目次

メンバー型

名前 定義
type T に cv 修飾子を適用した型

ヘルパー型

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; };
**注記**: 提供されたHTMLコード内のC++コードは
タグ内に含まれているため、翻訳対象外です。HTMLタグ・属性およびC++固有の用語もすべて原文のまま保持されています。

注記

これらの変換特性は、テンプレート引数推論において non-deduced contexts を確立するために使用できます:

template<class T>
void f(const T&, const T&);
template<class T>
void g(const T&, std::add_const_t<T>&);
f(4.2, 0); // エラー: 'T' に対して矛盾する型が推論されました
g(4.2, 0); // OK: g<double> を呼び出します

#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修飾されているかどうかをチェックする
(クラステンプレート)
型がvolatile修飾されているかどうかをチェックする
(クラステンプレート)
指定された型から const および/または volatile 指定子を除去する
(クラステンプレート)
(C++17)
引数への const 参照を取得する
(関数テンプレート)