📜  C++中的is_standard_layout模板

📅  最后修改于: 2021-05-30 08:39:54             🧑  作者: Mango

C++ STL的std :: is_standard_layout模板用于检查类型是否为标准布局。它返回一个显示相同值的布尔值。

语法

template < class T > struct is_standard_layout;

参数:该模板包含单个参数T(Trait类),以检查T是否为标准布局类型。

返回值:该模板返回一个布尔值,如下所示:

  • True :如果类型是标准布局类型。
  • False :如果类型是非标准布局类型。

下面的程序说明了C++ STL中的std :: is_standard_layout模板:

程序1 :将Struct,Class和Union组合在一起。

// C++ program to illustrate
// std::is_standard_layout  template
  
#include 
#include 
using namespace std;
  
// Class with local variable
class gfg {
    int variab;
};
  
// Structure with local variable
struct sam {
    int variab;
  
private:
    int variab_priv;
};
  
// Empty union
union raj {
};
  
// Driver code
int main()
{
    cout << boolalpha;
    cout << "Is gfg class a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is structure sam a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is union raj a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is datatype char a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is integer array 'int a[10]' a standard layout: "
         << is_standard_layout::value << '\n';
  
    return 0;
}
输出:
Is gfg class a standard layout: true
Is structure sam a standard layout: false
Is union raj a standard layout: true
Is datatype char a standard layout: true
Is integer array 'int a[10]' a standard layout: true

计划2 :带课

// C++ program to illustrate
// std::is_standard_layout  template
  
#include 
#include 
using namespace std;
  
// Class with local variable
class gfg {
    int variab;
};
  
// class with local variable
class sam {
    int variab;
  
private:
    int variab_priv;
};
  
// Empty class
class raj {
};
  
// Driver code
int main()
{
    cout << boolalpha;
    cout << "Is gfg class a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is structure sam a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is union raj a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is pointer 'int(gfg::*)' a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is pointer 'int *' a standard layout: "
         << is_standard_layout::value << '\n';
    cout << "Is pointer 'nullptr_t' a standard layout: "
         << is_standard_layout::value << '\n';
  
    return 0;
}
输出:
Is gfg class a standard layout: true
Is structure sam a standard layout: true
Is union raj a standard layout: true
Is pointer 'int(gfg::*)' a standard layout: true
Is pointer 'int *' a standard layout: true
Is pointer 'nullptr_t' a standard layout: true
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”