std::add_cv, std::add_const, std::add_volatile
提供: 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以上) |
追加された cv 修飾を持つ (T
が関数、参照、またはその cv 修飾をすでに持つ場合を除く) ことを除いて T
と同じ型であるメンバ型 type
が提供されます。
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 {typedefconstvolatile T type;}; template<class T>struct add_const {typedefconst T type;}; template<class T>struct add_volatile {typedefvolatile T type;}; |
[編集]例
Run this code
#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()constvolatile{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 な参照を取得します (関数テンプレート) |