當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ is_polymorphic用法及代碼示例

C++ STL 的 std::is_polymorphic 模板用於檢查類型是否為多態類類型。它返回一個顯示相同的布爾值。

用法

template < class T > struct is_polymorphic;

參數:此模板包含單個參數 T(Trait 類)以檢查 T 是否為多態類類型。

返回值:此模板返回一個布爾值,如下所示:

  • True:如果類型是多態類。
  • False:如果類型是非多態類。

以下示例程序旨在說明 C++ STL 中的 std::is_polymorphic 模板:



程序1:


// C++ program to illustrate
// std::is_polymorphic template
  
#include <iostream>
#include <type_traits>
using namespace std;
  
struct gfg {
    virtual void foo();
};
  
struct geeks:gfg {
};
  
class raj {
    virtual void foo() = 0;
};
  
struct sam:raj {
};
  
int main()
{
    cout << boolalpha;
    cout << "is_polymorphic:"
         << '\n';
    cout << "gfg:"
         << is_polymorphic<gfg>::value
         << '\n';
    cout << "geeks:"
         << is_polymorphic<geeks>::value
         << '\n';
    cout << "raj:"
         << is_polymorphic<raj>::value
         << '\n';
    cout << "sam:"
         << is_polymorphic<sam>::value
         << '\n';
  
    return 0;
}
輸出:
is_polymorphic:
gfg:true
geeks:true
raj:true
sam:true

程序2:


// C++ program to illustrate
// std::is_polymorphic template
  
#include <iostream>
#include <type_traits>
using namespace std;
  
struct gfg {
    int m;
};
  
struct sam {
    virtual void foo() = 0;
};
  
class raj:sam {
};
  
int main()
{
    cout << boolalpha;
    cout << "is_polymorphic:"
         << '\n';
    cout << "gfg:"
         << is_polymorphic<gfg>::value
         << '\n';
    cout << "sam:"
         << is_polymorphic<sam>::value
         << '\n';
    cout << "raj:"
         << is_polymorphic<raj>::value
         << '\n';
  
    return 0;
}
輸出:
is_polymorphic:
gfg:false
sam:true
raj:true



相關用法


注:本文由純淨天空篩選整理自rajasethupathi大神的英文原創作品 is_polymorphic template in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。