std:: is_const
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<type_traits>
|
||
|
template
<
class
T
>
struct is_const ; |
(C++11以降) | |
std::is_const
は
UnaryTypeTrait
です。
T
がconst修飾型(つまり
const
または
const
volatile
)の場合、メンバー定数
value
は
true
に等しくなります。それ以外の型の場合、
value
は
false
になります。
プログラムが
std::is_const
または
std::is_const_v
に対する特殊化を追加する場合、動作は未定義です。
目次 |
テンプレートパラメータ
| T | - | チェックする型 |
ヘルパー変数テンプレート
|
template
<
class
T
>
constexpr bool is_const_v = is_const < T > :: value ; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
|
value
[static]
|
true
(
T
がconst修飾型の場合)、
false
(それ以外の場合)
(public static member constant) |
メンバ関数
|
operator bool
|
オブジェクトを
bool
に変換し、
value
を返す
(public member function) |
|
operator()
(C++14)
|
value
を返す
(public member function) |
メンバ型
| 型 | 定義 |
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 {}; |
例
#include <type_traits> static_assert(std::is_same_v<const int*, int const*>, "ポインタ内部ではconst修飾子は強く結びつくことを忘れないでください。"); static_assert(!std::is_const_v<int>); static_assert(std::is_const_v<const int>); static_assert(!std::is_const_v<int*>); static_assert(std::is_const_v<int* const>, "なぜなら、ポインタ自体は変更できないが、指し示すintは変更可能だからです。"); static_assert(!std::is_const_v<const int*>, "なぜなら、ポインタ自体は変更可能だが、指し示すintは変更できないからです。"); static_assert(!std::is_const_v<const int&>); static_assert(std::is_const_v<std::remove_reference_t<const int&>>); struct S { void foo() const {} void bar() const {} }; int main() { // constメンバ関数は異なる方法でconstとなります: static_assert(!std::is_const_v<decltype(&S::foo)>, "なぜなら&S::fooはポインタだからです。"); using S_mem_fun_ptr = void(S::*)() const; S_mem_fun_ptr sfp = &S::foo; sfp = &S::bar; // OK、再ポイント可能 static_assert(!std::is_const_v<decltype(sfp)>, "なぜならsfpは同じポインタ型であり、再ポイント可能だからです。"); const S_mem_fun_ptr csfp = &S::foo; // csfp = &S::bar; // エラー static_assert(std::is_const_v<decltype(csfp)>, "なぜならcsfpは再ポイントできないからです。"); }
関連項目
|
(C++11)
|
型がvolatile修飾されているかどうかをチェックする
(クラステンプレート) |
|
(C++17)
|
引数への
const
参照を取得する
(関数テンプレート) |