📜  C++中的boost is_pointer模板

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

Boost库的is_pointer模板用于检查给定类型是否为指针。它返回一个显示相同值的布尔值。

头文件:

#include "boost/type_traits.hpp"
 or 
#include "boost/type_traits/is_pointer.hpp"

模板类别:

template 
struct is_pointer : public true_type-or-false_type {};

如果T是指针类型,则从true_type继承,否则从false_type继承。

句法:

boost::is_pointer::value
boost::is_pointer::value_type

参数:此模板接受单个参数T(特质类),以检查T是否为指针类型。

接受的参数:此模板接受以下参数:

typename T
T *volatile
T *const volatile
T *const
T *

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

  • True:如果类型是指针值。
  • False:如果类型是非指针值。

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

程序1:

// C++ program to illustrate
// std::is_floating_point template
#include 
#include 
#include 
using namespace std;
  
// Main Program
int main()
{
    cout << "is_pointer_type: \n";
    cout << "int *: "
         << boost::is_pointer::value << "\n";
    cout << "char *: "
         << boost::is_pointer::value
         << "\n";
  
    // Pointer to integer
    int* a;
    cout << "pointer to integer: "
         << boost::is_pointer::value
         << "\n";
  
    // Pointer to 1D array
    int* b = new int[29];
    cout << "pointer to 1-D array: "
         << boost::is_pointer::value
         << "\n";
  
    // 1-D array
    int c[20];
    cout << "1-D array :"
         << boost::is_pointer::value
         << "\n";
  
    return 0;
}
输出:
is_pointer_type: 
int *: 1
char *: 1
pointer to integer: 1
pointer to 1-D array: 1
1-D array :0

程式2:

// C++ program to illustrate
// std::is_floating_point template
#include 
#include 
#include 
using namespace std;
  
// Function with an integer
// argument and void return type
void fun(int x)
{
    cout << "value of x is : " << x << "\n";
}
  
// A Structure
struct A {
    int x;
    char a[8];
};
  
// A Class
class student {
    int roll;
    string name;
  
public:
    // Student Class Constructor
    student(int x, string y)
    {
        roll = x;
        name = y;
    }
  
    // Member function to get the
    // name of a student
    string get_name()
    {
        return name;
    }
  
    // Member function to get the
    // roll number of a student
    int get_roll()
    {
        return roll;
    }
};
  
// Main Program
int main()
{
    cout << "is_pointer_type: \n";
  
    // Pointer to function
    void (*ptr)(int) = &fun;
    cout << "pointer to fun: "
         << boost::is_pointer::value
         << "\n";
  
    // Instance of A
    A obj;
    cout << "instance of a structure: "
         << boost::is_pointer::value
         << "\n";
  
    // Pointer to instance of A
    A* obj2;
    cout << "pointer to instance of a structure: "
         << boost::is_pointer::value
         << "\n";
  
    // Instance of student
    student s(11840220, "GeeksforGeeks");
    cout << "instance of a class: "
         << boost::is_pointer::value
         << "\n";
  
    // Pointer to instance of student
    student* S;
    S = &s;
    cout << "pointer to instance of a class: "
         << boost::is_pointer::value
         << "\n";
  
    return 0;
}
输出:
is_pointer_type: 
pointer to fun: 1
instance of a structure: 0
pointer to instance of a structure: 1
instance of a class: 0
pointer to instance of a class: 1
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”