C++ STL 的 std::is_empty 模板用於檢查給定類型是否為空。此方法返回一個布爾值,顯示給定類型是否為空。
用法:
template < class T > struct is_empty;
參數:此模板包含單個參數 T(Trait 類),用於標識 T 是否為空類型。
返回值:此模板返回一個布爾值,如下所示:
- True:如果類型為空。
- False:如果類型不為空。
以下示例程序旨在說明 C++ STL 中的 std::is_empty 模板:
程序1::使用結構
// C++ program to illustrate
// std::is_empty template
#include <iostream>
#include <type_traits>
using namespace std;
// empty struct
struct GFG1 {
};
// struct with local variable
struct GFG2 {
int variab;
};
// Struct with global variable
struct GFG3 {
static int variab;
};
// Struct with virtual destructor
struct GFG4 {
virtual ~GFG4();
};
// Driver code
int main()
{
cout << boolalpha;
cout << "Is GFG1 empty:"
<< is_empty<GFG1>::value
<< '\n';
cout << "Is GFG2 empty:"
<< is_empty<GFG2>::value
<< '\n';
cout << "Is GFG3 empty:"
<< is_empty<GFG3>::value
<< '\n';
cout << "Is GFG4 empty:"
<< is_empty<GFG4>::value
<< '\n';
return 0;
}
輸出:
Is GFG1 empty:true Is GFG2 empty:false Is GFG3 empty:true Is GFG4 empty:false
程序2::使用類
// C++ program to illustrate
// std::is_empty template
#include <iostream>
#include <type_traits>
using namespace std;
// empty class
class GFG1 {
};
// class with local variable
class GFG2 {
int variab;
};
// class with global variable
class GFG3 {
static int variab;
};
// class with virtual destructor
class GFG4 {
virtual ~GFG4();
};
int main()
{
cout << boolalpha;
cout << "Is GFG1 empty:"
<< is_empty<GFG1>::value
<< '\n';
cout << "Is GFG2 empty:"
<< is_empty<GFG2>::value
<< '\n';
cout << "Is GFG3 empty:"
<< is_empty<GFG3>::value
<< '\n';
cout << "Is GFG4 empty:"
<< is_empty<GFG4>::value
<< '\n';
return 0;
}
輸出:
Is GFG1 empty:true Is GFG2 empty:false Is GFG3 empty:true Is GFG4 empty:false
相關用法
- C++ is_rvalue_reference用法及代碼示例
- C++ is_fundamental用法及代碼示例
- C++ is_pointer用法及代碼示例
- C++ is_class用法及代碼示例
- C++ std::is_enum模板用法及代碼示例
- C++ std::is_integral模板用法及代碼示例
- C++ is_arithmetic用法及代碼示例
- C++ is_lvalue_reference用法及代碼示例
- C++ is_reference用法及代碼示例
- C++ is_scalar用法及代碼示例
注:本文由純淨天空篩選整理自rajasethupathi大神的英文原創作品 is_empty template in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。