当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。