std:: is_empty
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<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 > |
注記
空の基底クラスからの継承は、通常、 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 { [[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)
|
型が非共用体クラス型かどうかをチェックする
(クラステンプレート) |