您的位置:首页 > 运维架构

boost::optional<T>

2016-04-22 22:28 295 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: