名前空間
変種
操作

std::is_empty

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型のプロパティ
(C++11)
is_empty
(C++11)
(C++14)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
関係とプロパティクエリ
型の変更
(C++11)(C++11)(C++11)
型の変換
(C++11)(C++23で非推奨)
(C++11)(C++23で非推奨)
(C++11)
(C++11)(C++20まで*)(C++17)

(C++11)
(C++17)
コンパイル時有理数演算
コンパイル時整数シーケンス
 
ヘッダ <type_traits> で定義
template< class T >
struct is_empty;
(C++11以降)

std::is_emptyUnaryTypeTrait です。

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
(公開静的メンバ定数)

メンバ関数

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++ standard allow "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<G>); // holds only unnamed bit-fields of zero width
 
int main()
{
    std::cout << std::boolalpha;
    std::cout << "F: " << std::is_empty_v<F> << '\n'; // the result is ABI-dependent
}

実行結果の例

F: true

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2015 C++11 次に該当する場合、動作は未定義でした。
T が未定義の共用体型
この場合、基底特性は
std::false_type

[編集] 関連項目

(C++11)
型が共用体でないクラス型であるかをチェックする
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)