এই আর্টিকেলে আমরা C++ STL-এ std::is_empty টেমপ্লেটের কাজ, সিনট্যাক্স এবং উদাহরণ নিয়ে আলোচনা করব।
is_empty হলো একটি টেমপ্লেট যা <type_traits> হেডার ফাইলের অধীনে আসে। এই টেমপ্লেটটি ব্যবহার করা হয় দেওয়া ক্লাস T একটি খালি ক্লাস কিনা তা চেক করার জন্য।
খালি ক্লাস কী?
একটি ক্লাসকে খালি বলা হয়, যখন ক্লাসে কোনো ডেটা সংরক্ষিত থাকে না। খালি ক্লাস নিচের শর্তগুলো পূরণ করে -
- শূন্য দৈর্ঘ্যের বিট ফিল্ড ছাড়া কোনো নন-স্ট্যাটিক মেম্বার থাকা উচিত নয়।
- কোনো ভার্চুয়াল বেস ক্লাস বা ভার্চুয়াল ফাংশন থাকা উচিত নয়।
- কোনো বেস ক্লাস থাকা উচিত নয়।
সিনট্যাক্স
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