std::is_const
Определён в заголовочном файле <type_traits> | ||
|---|---|---|
template< class T > struct is_const; | (начиная с C++11) |
std::is_const — это унарное типовое свойство.
Если T является типом с квалификатором const (то есть const или constvolatile), предоставляет константу-член 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 в противном случае (открытая статическая константа-член) |
Функции-члены
|
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 {};
|
Пример
#include <type_traits>
static_assert(std::is_same_v<const int*, int const*>,
"Remember, constness binds tightly inside pointers.");
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>,
"Because the pointer itself can't be changed but the int pointed at can.");
static_assert(!std::is_const_v<const int*>,
"Because the pointer itself can be changed but not the int pointed at.");
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()
{
// A const member function is const in a different way:
static_assert(!std::is_const_v<decltype(&S::foo)>,
"Because &S::foo is a pointer.");
using S_mem_fun_ptr = void(S::*)() const;
S_mem_fun_ptr sfp = &S::foo;
sfp = &S::bar; // OK, can be re-pointed
static_assert(!std::is_const_v<decltype(sfp)>,
"Because sfp is the same pointer type and thus can be re-pointed.");
const S_mem_fun_ptr csfp = &S::foo;
// csfp = &S::bar; // Error
static_assert(std::is_const_v<decltype(csfp)>,
"Because csfp cannot be re-pointed.");
}
См. также
|
(C++11) | проверяет, имеет ли тип квалификатор volatile (шаблон класса) |
|
(C++17) | получает ссылку с квалификатором const на свой аргумент (шаблон функции) |
|
(C++26) | проверяет, представляет ли отражение тип const или тип функции с квалификатором const (функция) |
|
(C++26) | проверяет, представляет ли отражение тип с квалификатором const (функция) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/types/is_const