boost::optional
#include "stdafx.h"
#include <iostream>
#include <boost/thread.hpp>
#include <boost/atomic.hpp>
#include <boost/range/algorithm/equal.hpp>
#include <boost/algorithm/string.hpp>
#include <string>
#include <boost/assign/std/vector.hpp> // for 'operator+=()'
#include <boost/integer/integer_mask.hpp>
#include <boost/bimap.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/utility.hpp>
#include <boost/assert.hpp>
#include <boost/optional.hpp>
using namespace std;
using namespace boost::assign; // bring 'operator+=()' into scope
#include <fstream>
int main()
{
try
{
boost::optional<int> value_1;
bool bool_1 = value_1.is_initialized();
//下面的语句会抛出异常
//int b = value_1.value();
value_1 = 3;
bool bool_2 = value_1.is_initialized();
BOOST_ASSERT( 3 == value_1.value() );
BOOST_ASSERT( 3 == *value_1 );
BOOST_ASSERT( 3 == value_1 );
}
catch (boost::bad_optional_access ex)
{
cout << "bad_optional_access : access the uninitialized value." << endl;
}
return 0;
}