CHECK_NOTNULL template

#include <iostream>
using namespace std;

#define CHECK_NOTNULL(val) \
    CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))

template <typename T>
T* CheckNotNull(const char* file, int line, const char *names, T* ptr)
{
    if (ptr == NULL)
    {
        cout << names << endl;
    }
    return ptr;
}

int main(){
    int *p=NULL;
    int *q=CHECK_NOTNULL(p);
    CHECK_NOTNULL(q);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/o1101574955/article/details/77801919