Boost C++ Libraries: Ticket #13338: checked_delete问题 https://svn.boost.org/trac10/ticket/13338 <p> 环境: Visual studio 2017 boost库编译选项: msvc141,multi,win64,debug,shared demo工程设置: debug, win32 </p> <p> 问题源: 《Beyond the C++ STL: an introduction to boost》书上Part I, Library 3, checked_delete章节 如果把 deleter.h, deleter.cpp, to_be_deleted.h三个文件内容整合到一个文件里, 编译后会提示 " warning C4150: 删除指向不完整“to_be_deleted”类型的指针;没有调用析构函数" 运行后调用 deleter::do_it 时会间接调用到 ~to_be_deleted()。 调用 deleter::delete_it 时则不会间接调用到 ~to_be_deleted()。 最后调用完 ~to_be_deleted()程序结束时才报一个异常错误。 很奇怪的地方啊!看来是编译器工作方式的不同导致的吧。。。 </p> <p> code like this: </p> <p> <em> deleter.h class to_be_deleted; class deleter { public: </em></p> <blockquote> <p> void delete_it(to_be_deleted* p); void do_it(to_be_deleted* p); </p> </blockquote> <p> }; <em> deleter.cpp </em>#include "deleter.h" #include "boost/checked_delete.hpp" void deleter::delete_it(to_be_deleted* p) { </p> <blockquote> <p> delete p; </p> </blockquote> <p> } void deleter::do_it(to_be_deleted* p) { </p> <blockquote> <p> boost::checked_delete(p); <em> typedef char type_must_be_complete[sizeof(T)]; 所有代码放入一个文件,这句居然能编译通过?? </em></p> </blockquote> <p> } <em> to_be_deleted.h #include &lt;iostream&gt; class to_be_deleted { public: </em></p> <blockquote> <p> ~to_be_deleted() { </p> <blockquote> <p> std::cout &lt;&lt; </p> <blockquote> <p> "I'd like to say important things here, please."; </p> </blockquote> </blockquote> <p> } </p> </blockquote> <p> }; <em> Test application </em>#include "deleter.h" <em>#include "to_be_deleted.h" int main() { </em></p> <blockquote> <p> to_be_deleted* p = new to_be_deleted; deleter d; d.delete_it(p); d.do_it(p); </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13338 Trac 1.4.3