1. 程式人生 > >Boost中Core模組的checked_delete用法

Boost中Core模組的checked_delete用法

標頭檔案

boost/core/checked_delete.hpp

作用

delete 物件指標,或 物件陣列指標,如果物件是不完整定義型別或者空指標,則 編譯期間 出錯。

何為不完整型別定義,就是隻有 型別前置宣告,沒有引入標頭檔案。

舉例

//base.h
#include <iostream>
using namespace std;

class Base
{
public:
    Base()
    {}
    virtual ~Base()    
    {
        cout<<"~Base()"<<endl;
    }
};

//drive.h
#include <iostream>
using namespace std;

class Drive : public Base
{
public:
    Drive()
    {}
    virtual ~Drive()    
    {
        cout<<"~Drive()"<<endl;
    }
};

//deleteobject.h
class Base;
class DeleteObject
{
public:
    DeleteObject()
    {}
    ~DeleteObject()    
    {
        
    }
    
    void deleteObj(Base* base);
    
};

//deleteobject.cpp
void DeleteObject::deleteObj(Base* base)
{
    //並不會 delete Base物件,Base並沒有執行。
    //因為 deleteobject.cpp 並沒有 引言 base.h
    delete base;
}

//main.cpp
#include "drive.h"
#include "deleteobject.h"

int main(int argc, char* [])
{
    Base* base = new Drive();
    DeleteObject deOb;
    
    //
    deOb.deleteObj(base);
    return 0;
}

為了 避免 以上情況 發生,就需要 使用 checked_delete

舉例

刪除 空指標

#include <boost/checked_delete.hpp>  // for checked_delete

//  This program demonstrates compiler errors when trying to delete an
//  incomplete type.

namespace
{
    class Incomplete;
}

int main()
{
    Incomplete * p = 0;
    boost::checked_delete(p);          // should cause compile time error
    return 0;
}   // main

刪除 空指標陣列

#include <boost/checked_delete.hpp>  // for checked_delete

//  This program demonstrates compiler errors when trying to delete an
//  incomplete type.

namespace
{
    class Incomplete;
}

int main()
{
    Incomplete * p = 0;
    boost::checked_array_delete(p);    // should cause compile time error
    return 0;
}   // main

正常刪除

#include <boost/core/checked_delete.hpp>
#include <boost/core/lightweight_test.hpp>

struct X
{
    static int instances;

    X()
    {
        ++instances;
    }

    ~X()
    {
        --instances;
    }

private:

    X( X const & );
    X & operator=( X const & );
};

int X::instances = 0;

int main()
{
    BOOST_TEST( X::instances == 0 );

    {
        X * p = new X;

        BOOST_TEST( X::instances == 1 );

        boost::checked_delete( p );

        BOOST_TEST( X::instances == 0 );
    }

    {
        X * p = new X[ 3 ];

        BOOST_TEST( X::instances == 3 );

        boost::checked_array_delete( p );

        BOOST_TEST( X::instances == 0 );
    }

    return boost::report_errors();
}

原始碼

namespace boost
{

// verify that types are complete for increased safety

template<class T> inline void checked_delete(T * x)
{
    // intentionally complex - simplification causes regressions
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete x;
}

template<class T> inline void checked_array_delete(T * x)
{
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete [] x;
}

template<class T> struct checked_deleter
{
    typedef void result_type;
    typedef T * argument_type;

    void operator()(T * x) const
    {
        // boost:: disables ADL
        boost::checked_delete(x);
    }
};

template<class T> struct checked_array_deleter
{
    typedef void result_type;
    typedef T * argument_type;

    void operator()(T * x) const
    {
        boost::checked_array_delete(x);
    }
};

} // namespace boost