Namespaces
Variants

std:: is_empty

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
is_empty
(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_empty ;
(C++11부터)

std::is_empty UnaryTypeTrait 입니다.

만약 T 가 빈 타입인 경우(즉, 크기가 0인 비트 필드 외에 비정적 데이터 멤버가 없고, 가상 함수가 없으며, 가상 기본 클래스가 없고, 비어있지 않은 기본 클래스가 없는 비-유니온 클래스 타입), 멤버 상수 value true 로 제공합니다. 다른 모든 타입에 대해서는 value false 입니다.

만약 T 가 불완전한 비공용체 클래스 타입인 경우, 동작은 정의되지 않습니다.

프로그램이 std::is_empty std::is_empty_v 에 대한 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

템플릿 매개변수

T - 확인할 타입

헬퍼 변수 템플릿

template < class T >
constexpr bool is_empty_v = is_empty < T > :: value ;
(C++17부터)

std:: integral_constant 로부터 상속됨

멤버 상수

value
[static]
true 만약 T 가 빈 클래스 타입인 경우, 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 >

참고 사항

빈 기본 클래스로부터 상속하는 것은 일반적으로 빈 기본 최적화 덕분에 클래스의 크기를 증가시키지 않습니다.

std::is_empty<T> 및 다른 모든 형식 특질은 빈 클래스입니다.

예제

#include <iostream>
#include <type_traits>
struct A {};
static_assert(std::is_empty_v<A> == true);
struct B { int m; };
static_assert(std::is_empty_v<B> == false);
struct C { static int m; };
static_assert(std::is_empty_v<C> == true);
struct D { virtual ~D(); };
static_assert(std::is_empty_v<D> == false);
union E {};
static_assert(std::is_empty_v<E> == false);
struct F { [[no_unique_address]] E e; };
struct G
{
    int:0;
    // C++ 표준은 "특별한 경우로, 너비가 0인 이름 없는 비트 필드는
    // 다음 비트 필드의 정렬을 할당 단위 경계에 지정합니다.
    // 이름 없는 비트 필드를 선언할 때만 너비가 0일 수 있습니다."
};
static_assert(std::is_empty_v<G>); // 너비가 0인 이름 없는 비트 필드만 포함하는 경우에만 성립
int main()
{
    std::cout << std::boolalpha;
    std::cout << "F: " << std::is_empty_v<F> << '\n'; // 결과는 ABI에 의존적
}

가능한 출력:

F: true

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2015 C++11 T 가 불완전한 공용체 타입인 경우
동작이 정의되지 않음
이 경우 기본 특성은
std::false_type

참고 항목

(C++11)
타입이 비-유니온 클래스 타입인지 검사합니다
(클래스 템플릿)