types_is_empty
types_is_empty (std::is_empty)
std::is_empty는 주어진 타입이 빈 타입(empty type)인지 확인하는 C++ 타입 특성(type trait)이에요. 이 페이지에서는 std::is_empty의 정의, 템플릿 매개변수, 헬퍼 변수 템플릿, 상속된 멤버, 예제 등을 자세히 다룰게요.
출처: cppreference
본문
정의
<type_traits> 헤더에 정의되어 있어요.
<type_traits> 헤더에 정의됨 |
||
|---|---|---|
template < class T > struct is_empty ; |
(C++11부터) |
std::is_empty는 UnaryTypeTrait예요.
만약 T가 빈 타입이라면, 즉 다음 조건을 모두 만족하는 비-공용체 클래스 타입이라면 멤버 상수 value는 true가 돼요:
- 크기가 0인 비트 필드 외에는 비정적 데이터 멤버가 없음
- 가상 함수가 없음
- 가상 기본 클래스가 없음
- 비어 있지 않은 기본 클래스가 없음
다른 모든 타입에 대해서는 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] |
T가 빈 클래스 타입이면 true, 아니면 false (공용 정적 멤버 상수) |
|---|
멤버 함수
operator bool |
객체를 bool로 변환하고 value를 반환해요 (공용 멤버 함수) |
|---|---|
operator() (C++14) |
value를 반환해요 (공용 멤버 함수) |
멤버 타입
| Type | Definition |
|---|---|
value_type |
bool |
type |
std :: integral_constant < bool , value > |
참고 사항
빈 기본 클래스에서 상속하는 것은 일반적으로 empty base optimization 덕분에 클래스의 크기를 증가시키지 않아요.
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
{
int:0;
// C++ standard allows "as a special case, an unnamed bit-field with a width of zero
// specifies alignment of the next bit-field at an allocation unit boundary.
// Only when declaring an unnamed bit-field may the width be zero."
};
static_assert(std::is_empty_v<F>); // holds only unnamed bit-fields of zero width
struct G { [[no_unique_address]] E e; };
int main()
{
std::cout << std::boolalpha;
std::cout << "G: " << std::is_empty_v<G> << '\n'; // the result is ABI-dependent
}
가능한 출력:
G: true
결함 보고서
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었어요.
| DR | 적용 대상 | 발표된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2015 | C++11 | T가 불완전한 공용체 타입이면 동작이 정의되지 않았음 |
이 경우 기본 특성은 std::false_type이에요 |
같이 보기
is_class (C++11) |
타입이 비-공용체 클래스 타입인지 확인해요 (클래스 템플릿) [edit] |
|---|---|
is_empty_type (C++26) |
반영된 타입이 클래스(공용체 제외) 타입이고 비정적 데이터 멤버가 없는지 확인해요 (함수) [edit] |