이 기사에서는 C++ STL에서 std::is_final 템플릿의 작동, 구문 및 예제에 대해 논의할 것입니다.
is_final은
C++의 최종 클래스란 무엇입니까?
최종 지정자로 클래스를 선언하면 최종 클래스라고 합니다. Final 클래스는 다른 클래스를 생성하기 위해 확장할 수 없는 특별한 종류의 클래스입니다. C++에서는 클래스를 final로 만들기 위해 클래스를 친구로 만든 다음 해당 클래스를 가상으로 상속하여 해당 클래스를 확장 불가능하게 만듭니다.
최종 수업의 예
class final_abc; //Class which is to be made final
class abc {
private:
abc(){cout<<"abc constructor";}
friend class final_abc;
};
class final_abc : virtual abc //made it final class. {
public:
final_abc(){cout<<"Final class constructor";}
};
class derive : public final_abc; //Error can't be extended 구문
template <class T> is_final;
매개변수
템플릿은 T 타입의 파라미터만 가질 수 있으며, 주어진 타입이 최종 클래스 타입인지 확인합니다.
반환 값
Boolean 값을 반환하고, 주어진 유형이 최종 클래스이면 true, 주어진 유형이 최종 클래스가 아니면 false를 반환합니다.
예시
Input: class final_abc;
class abc { friend class final_abc; };
class final_abc : virtual abc{ };
is_final<abc>::value;
Output: False
Input: class final_abc;
class abc
{ friend class final_abc; };
class final_abc : virtual abc{ };
is_final<final_abc>::value;
Output: True 예시
#include <iostream>
#include <type_traits>
using namespace std;
class TP {
//Same if it will be a structure instead of a class
};
class T_P final {
//Same if it will be a structure instead of a class
};
int main() {
cout << boolalpha;
cout << "Using is_final";
cout << "\nTutorials Point: "<<is_final<TP>::value;
cout << "\nT_P Final: "<<is_final<T_P>::value;
cout << "\ncheck for char: "<<is_final<int>::value;
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Using is_final Tutorials Point: false T_P Final: true check for char: false
예시
#include <iostream>
#include <type_traits>
using namespace std;
union TP {
//Union
};
union T_P final {
//Union
};
int main() {
cout << boolalpha;
cout << "Using is_final";
cout << "\nTutorials Point: "<<is_final<TP>::value;
cout << "\nT_P Final: "<<is_final<T_P>::value;
cout << "\ncheck for char: "<<is_final<int>::value;
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Using is_final Tutorials Point: false T_P Final: true check for char: false