(C++) SmartPointerExample2

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) SmartPointerExample2

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppSmartPointerExample2/CppSmartPointerExample2.pro

 


TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra unix {   QMAKE_CXXFLAGS += -Werror } win32 {   INCLUDEPATH += ../../Libraries/boost_1_54_0 } SOURCES += main.cpp

 

 

 

 

 

./CppSmartPointerExample2/main.cpp

 


#include <memory> #include <boost/scoped_ptr.hpp> #include <boost/shared_ptr.hpp> struct Test { int m_x; }; int main() {   {     std::shared_ptr<Test> p { new Test };     assert(p);     //p.reset(nullptr); //Invalid: std::shared_ptr cannot be reset to nullptr     //assert(!p);   }   {     std::unique_ptr<Test> p { new Test };     assert(p);     p.reset(nullptr); //Valid: std::unique_ptr can be reset to nullptr     assert(!p);   }   {     boost::shared_ptr<Test> p { new Test };     assert(p);     //p.reset(nullptr); //Invalid: boost::shared_ptr cannot be reset to nullptr     //assert(!p);   }   {     boost::scoped_ptr<Test> p { new Test };     assert(p);     p.reset(nullptr); //Valid: boost::scoped_ptr can be reset to nullptr     assert(!p);   } }