Namespaces
Variants

std:: is_reference

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
is_reference
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
Определено в заголовочном файле <type_traits>
template < class T >
struct is_reference ;
(начиная с C++11)

std::is_reference является UnaryTypeTrait .

Если T является ссылочным типом (lvalue-ссылка или rvalue-ссылка), предоставляет константу-член value равную true . Для любого другого типа value равна false .

Если программа добавляет специализации для std::is_reference или std::is_reference_v , поведение не определено.

Содержание

Параметры шаблона

T - тип для проверки

Шаблон вспомогательной переменной

template < class T >
constexpr bool is_reference_v = is_reference < T > :: value ;
(начиная с C++17)

Унаследовано от std:: integral_constant

Константы-члены

value
[static]
true если T является ссылочным типом, false в противном случае
(публичная статическая константа-член)

Функции-члены

operator bool
преобразует объект в bool , возвращает value
(публичная функция-член)
operator()
(C++14)
возвращает value
(публичная функция-член)

Типы-члены

Тип Определение
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

Смотрите также

проверяет, является ли тип lvalue reference
(шаблон класса)
проверяет, является ли тип rvalue reference
(шаблон класса)