C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C++のis_emptyテンプレート


この記事では、C++STLでのstd::is_emptyテンプレートの動作、構文、および例について説明します。

is_emptyは、ヘッダーファイルの下にあるテンプレートです。このテンプレートは、指定されたクラスTが空のクラスであるかどうかを確認するために使用されます。

空のクラスとは何ですか?

クラスにデータが格納されていない場合、クラスは空と呼ばれます。空のクラスは次の条件を満たす-

  • 長さ0のビットフィールド以外の非静的メンバーがあってはなりません。
  • 仮想基本クラスまたは仮想関数があってはなりません。
  • 基本クラスがない必要があります。

構文

template <class T>is_empty;

パラメータ

テンプレートにはクラスTのパラメータのみを含めることができ、クラスTが空のクラスであるかどうかを確認できます。

戻り値

ブール値を返します。指定された型が空のクラスの場合はtrue、指定された型が空のクラスでない場合はfalseです。

Input: class A{};
   is_empty<A>::value;
Output: true

Input: class B{ void fun() {} };
   is_empty<B>::value;
Output: true

#include <iostream>
#include <type_traits>
using namespace std;
class TP_1 {
};
class TP_2 {
   int var;
};
class TP_3 {
   static int var;
};
class TP_4 {
   ~TP_4();
};
int main() {
   cout << boolalpha;
   cout << "checking for is_empty template for a class with no variable: "<< is_empty<TP_1>::value;
   cout <<"\nchecking for is_empty template for a class with one variable: "<< is_empty<TP_2>::value;
   cout <<"\nchecking for is_empty template for a class with one static variable: "<< is_empty<TP_3>::value;
   cout <<"\nchecking for is_empty template for a class with constructor: "<< is_empty<TP_4>::value;
   return 0;
}

出力

上記のコードを実行すると、次の出力が生成されます-

checking for is_empty template for a class with no variable: true
checking for is_empty template for a class with one variable: false
checking for is_empty template for a class with one static variable: true
checking for is_empty template for a class with constructor: true

#include <iostream>
#include <type_traits>
using namespace std;
struct TP_1 {
};
struct TP_2 {
   int var;
};
struct TP_3 {
   static int var;
};
struct TP_4 {
   ~TP_4();
};
int main() {
   cout << boolalpha;
   cout << "checking for is_empty template for a structure with no variable: "<< is_empty<TP_1>::value;
   cout <<"\nchecking for is_empty template for a structure with one variable: "<< is_empty<TP_2>::value;
   cout <<"\nchecking for is_empty template for a structure with one static variable: "<< is_empty<TP_3>::value;
   cout <<"\nchecking for is_empty template for a structure with constructor: "<< is_empty<TP_4>::value;
   return 0;
}

出力

上記のコードを実行すると、次の出力が生成されます-

checking for is_empty template for a structure with no variable: true
checking for is_empty template for a structure with one variable: false
checking for is_empty template for a structure with one static variable: true
checking for is_empty template for a structure with constructor: true

  1. C++での構造とクラス

    C ++では、構造とクラスは基本的に同じです。しかし、いくつかの小さな違いがあります。これらの違いは以下のようなものです。 クラスメンバーはデフォルトでプライベートですが、構造体のメンバーはパブリックです。違いを確認するために、これら2つのコードを見てみましょう。 サンプルコード #include <iostream> using namespace std; class my_class {    int x = 10; }; int main() {    my_class my_ob;    cout <&l

  2. C++のローカルクラス

    関数内で宣言されたクラスは、その関数に対してローカルであるため、C++ではローカルクラスと呼ばれます。 ローカルクラスの例を以下に示します。 #include<iostream> using namespace std; void func() {    class LocalClass {    }; } int main() {    return 0; } 上記の例では、func()は関数であり、クラスLocalClassは関数内で定義されています。したがって、ローカルクラスとして知られています。 ローカルクラ