std::is_reference — cppreference.com
Материал из cppreference.com
<tbody> </tbody>
|
|
(начиная с C++11) | |
std::is_reference является UnaryTypeTrait.
Если T является ссылочным типом (ссылка lvalue или ссылка rvalue), предоставляет константу-элемент value, равную true. Для любого другого типа value равна false.
Поведение программы, добавляющей специализации для std::is_reference или std::is_reference_v (начиная с C++17) не определено.
Параметры шаблона
Шаблон вспомогательной переменной
<tbody> </tbody>
|
|
(начиная с C++17) | |
Унаследован от std::integral_constant
Константы элементы
true, если T является ссылочным типом, false иначе (public static константа-элемент) |
Функции-элементы
преобразует объект в bool, возвращает value (public функция-элемент) | |
возвращает value (public функция-элемент) |
Типы элементы
| Тип | Определение |
value_type
|
bool
|
type
|
std::integral_constant<bool, value>
|
Возможная реализация
template <class T> struct is_reference : std::false_type {}; template <class T> struct is_reference<T&> : std::true_type {}; template <class T> struct is_reference<T&&> : std::true_type {};
Пример
#include <iostream> #include <type_traits> class A {}; int main() { # define REF(x) << #x " ?: " << x << '\n' std::cout << std::boolalpha REF( std::is_reference_v<A> ) REF( std::is_reference_v<A&> ) REF( std::is_reference_v<A&&> ) REF( std::is_reference_v<long> ) REF( std::is_reference_v<long&> ) REF( std::is_reference_v<long&&> ) REF( std::is_reference_v<double*> ) REF( std::is_reference_v<double*&> ) REF( std::is_reference_v<double*&&> ); # undef REF }
Вывод:
std::is_reference_v<A> ?: false std::is_reference_v<A&> ?: true std::is_reference_v<A&&> ?: true std::is_reference_v<long> ?: false std::is_reference_v<long&> ?: true std::is_reference_v<long&&> ?: true std::is_reference_v<double*> ?: false std::is_reference_v<double*&> ?: true std::is_reference_v<double*&&> ?: true