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;
}
#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;
}
相关文章推荐
- LINUX内核学习总结
- Centos7修改主机名
- Linux最大文件打开数查看和修改
- Linux容器LXC学习之Namespace
- Apache Commons Collections使用方法
- Mac OS X中配置Apache
- Bash 快捷键
- Dubbo学习(八):监控中心Monitor
- Hadoop学习之第六章节:Hbase配置安装
- 进程监控与管理
- 用低级别账户启动nginx报错的原因
- 第七周作业 apache
- CentOS7安装与配置Tomcat-8
- OpenCV完美配置攻略
- Shell中创建序列和数组(list、array)的方法
- Linux下拷贝整个目录下的所有文件解决方案
- makefile shell下实现自动编译的脚本语言!
- linux系统结构
- opencv分别显示彩色图片各通道图片
- 【yii2从Apache迁移到nginx上访问报500错误】