std::is_const
提供: cppreference.com
ヘッダ <type_traits> で定義
|
||
template< class T > struct is_const; |
(C++11以上) | |
T
が const 修飾された型 (つまり const または const volatile) であれば、 true に等しいメンバ定数 value
が提供されます。 それ以外の型に対しては、 value
は false です。
is_const
または is_const_v
(C++17以上) に対して特殊化を追加するプログラムは未定義です。
目次 |
[編集] テンプレート引数
T | - | 調べる型 |
[編集] ヘルパー変数テンプレート
template< class T > inline constexpr bool is_const_v = is_const<T>::value; |
(C++17以上) | |
std::integral_constant から継承
メンバ定数
value [静的] |
T が const 修飾された型ならば true、そうでなければ false (パブリック静的メンバ定数) |
メンバ関数
operator bool |
オブジェクトを bool に変換します。 value を返します (パブリックメンバ関数) |
operator() (C++14) |
value を返します (パブリックメンバ関数) |
メンバ型
型 | 定義 |
value_type
|
bool
|
type
|
std::integral_constant<bool, value> |
[編集] ノート
T が参照型の場合、 is_const<T>::value は必ず false になります。 参照型かもしれない型の const 性を確認するには、参照を除去して is_const<typename remove_reference<T>::type> のようにします。
[編集] 実装例
template<class T> struct is_const : std::false_type {}; template<class T> struct is_const<const T> : std::true_type {}; |
[編集] 例
Run this code
#include <iostream> #include <type_traits> int main() { std::cout << std::boolalpha << std::is_const_v<int> << '\n' // false << std::is_const_v<const int> << '\n' // true << std::is_const_v<const int*> /*false*/ << " because the pointer itself can be changed but not the int pointed at\n" << std::is_const_v<int* const> /*true*/ << " because the pointer itself can't be changed but the int pointed at can\n" << std::is_const_v<const int&> << '\n' // false << std::is_const_v<std::remove_reference_t<const int&>> << '\n' // true ; }
出力:
false true false because the pointer itself can be changed but not the int pointed at true because the pointer itself can't be changed but the int pointed at can false true
[編集] 関連項目
(C++11) |
型が volatile 修飾されているかどうか調べます (クラステンプレート) |
(C++17) |
引数への const な参照を取得します (関数テンプレート) |