std::add_cv, std::add_const, std::add_volatile
From cppreference.com
| ヘッダ <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 と同じですが、(T が関数、参照、または既にこの cv 修飾子を持っている場合を除き) cv 修飾子が追加されています。
1) const と volatile の両方を追加します。
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; }; |
[編集] 注記
これらの変換特性は、テンプレート引数推論における 非推論コンテキスト を確立するために使用できます。
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); // error, deduced conflicting types for 'T' g(4.2, 0); // OK, calls 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修飾されているかをチェックする (クラステンプレート) |
| (C++11) |
型がvolatile修飾されているかをチェックする (クラステンプレート) |
| (C++11)(C++11)(C++11) |
与えられた型から const および/または volatile 修飾子を削除する (クラステンプレート) |
| (C++17) |
引数への const 参照を取得する (関数テンプレート) |