金庆的专栏
C++博客
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
78 随笔 :: 0 文章 :: 231 评论 :: 0 Trackbacks
公告
欢迎订阅
上海程序员
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(4)
给我留言
查看公开留言
查看私人留言
随笔分类
(87)
1. C/C++(41)
(rss)
2. 网游开发(5)
(rss)
3. Python(16)
(rss)
4. Linux/Unix(3)
(rss)
5. 软工与管理(12)
(rss)
6. 其它(10)
(rss)
随笔档案
(78)
2008年11月 (4)
2008年10月 (1)
2008年9月 (3)
2008年8月 (4)
2008年7月 (3)
2008年6月 (4)
2008年5月 (6)
2008年4月 (7)
2008年3月 (6)
2008年1月 (5)
2007年12月 (7)
2007年11月 (4)
2007年10月 (5)
2007年9月 (6)
2007年8月 (8)
2007年7月 (5)
相册
公告照片
搜索
积分与排名
积分 - 73358
排名 - 28
最新评论
1. re: 批量html转text
你好,我需要这个程序代码,可以发给我一份吗?
不胜感谢,我的邮箱 lychenhanqing@163.com
--Hanqing Chen
2. re: Boost.Python构建与测试HOWTO
同意 ! 相当烂
--temp2
3. re: 万王之王3即将推出
不能
--xoyojank
4. re: log4cxx配合boost::format
评论内容较长,点击标题查看
--cui
5. re: STL容器的赋值运算符可以更灵活吗?
可以自己写一个static。话说,提供这样的东西很不安全。
--陈梓瀚(vczh)
阅读排行榜
1. VC6正在被抛弃(2812)
2. STL与标准库(1788)
3. 比较C++数组元素引用的两种形式a[0]与0[a](1697)
4. VS2005编译libevent(1691)
5. 用 (*it).m 还是 it->m(1637)
评论排行榜
1. VC6正在被抛弃(28)
2. "multiple definition of" 错误(18)
3. ACE与ASIO之间关于Socket编程的比较(16)
4. 把private声明从.h文件移到.cpp文件(13)
5. 用const方法更改非mutable成员(10)
60天内阅读排行
1. STL容器的赋值运算符可以更灵活吗?(1059)
2. log4cxx配合boost::format(1044)
3. pthread_rwlock使用错误(894)
4. Commons SCXML 中模型的类结构(795)
5. 万王之王3即将推出(46)
boost::static_assert
原文地址:
http://www.smth.edu.cn/bbsanc.php?path=%2Fgroups%2Fdevelop.faq%2FProgramming%2Flanguages%2Fc_cpp%2Fboost%2FM.1021441404.O0
发信人: flier (小海 (:好日子不多了:)), 信区: Programming
标 题: boost::static_assert
发信站: BBS 水木清华站 (Wed May 15 13:43:35 2002)
boost::static_assert
BOOST_STATIC_ASSERT的
使用
BOOST_STATIC_ASSERT是一个简单但常用的宏,顾名思义起到编译期
断言的功效,可以通过它,在编译时对开发环境以及类型定义进行检查。
此类型检测对程序运行时无任何效率和空间上的影响。
例如在namespace中加入
namespace my_conditions {
BOOST_STATIC_ASSERT(sizeof(int) * CHAR_BIT >= 32);
BOOST_STATIC_ASSERT(WCHAR_MIN >= 0);
} // namespace my_conditions
确保int类型至少32位,wchar_t类型为unsigned。
也可以在模板函数中检测入口参数的类型是否符合要求
template <class RandomAccessIterator >
RandomAccessIterator foo(RandomAccessIterator from, RandomAccessIterator to)
{
// this template can only be used with
// random access iterators...
typedef typename std::iterator_traits
< RandomAccessIterator >::iterator_category cat;
BOOST_STATIC_ASSERT((boost::is_convertible
<cat, const std::random_access_iterator_tag&>::value));
//
// detail goes here...
return from;
}
确保foo函数的输入迭代子符合random access iterator的concept
此外还可以在模板类里面验证模板参数
template <class UnsignedInt>
class myclass
{
private:
BOOST_STATIC_ASSERT(sizeof(UnsignedInt) * CHAR_BIT >= 16);
BOOST_STATIC_ASSERT(std::numeric_limits<UnsignedInt>::is_specialized
&& std::numeric_limits<UnsignedInt>::is_integer
&& !std::numeric_limits<UnsignedInt>::is_signed);
public:
/* details here */
};
通过以上示例,我们可以总结出BOOST_STATIC_ASSERT的基本使用规律
首先是全局上对编译环境进行检测,看是否符合设计开发时的假定,这对程序的
可移植性有很大帮助。其次是在模板类、模板函数中,对作为模板参数的类型
进行检测,验证其是否符合设计时假设。最后是能够将gp中的concept概念贯彻
到代码中,使代码强制性与设计同步,并增强代码可读性。
BOOST_STATIC_ASSERT的
实现
在实现上,BOOST_STATIC_ASSERT宏利用c++规范中,对不完整类型
即不可实例化的类型,在对其进行sizeof运算时编译错误的特性,完成功能
大概的简化代码如下
template <bool x> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true>{};
template<int x> struct static_assert_test{};
#define BOOST_STATIC_ASSERT(B) \
typedef static_assert_test<sizeof(STATIC_ASSERTION_FAILURE<B> > \
boost_static_assert_typedef_ ## __LINE__
注意为了能够使用多个BOOST_STATIC_ASSERT,在类型命名时加入了
行号以区别。对namespace而言,因为同一namespace可能分布在多个不同的
头文件中,而不同头文件中可能在同一行使用BOOST_STATIC_ASSERT检测,
所以必须用一个namespace把断言检测隔离开,如上面例子所示。
此外,对于VC来说,在使用/ZI参数时,__LINE__宏会发生错误,
(参见MSDN中Q199057错误),好在VC会忽略typedef重复定义。
对某些不支持typedef此类特性的编译器,boost提供了使用enum的替代方案
附:boost/static_assert.hpp
// (C) Copyright John Maddock 2000.
// Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// See http://www.boost.org for most recent version including documentation.
/*
Revision history:
02 August 2000
Initial version.
*/
#ifndef BOOST_STATIC_ASSERT_HPP
#define BOOST_STATIC_ASSERT_HPP
#include <boost/config.hpp>
#ifdef __BORLANDC__
//
// workaround for buggy integral-constant expression support:
#define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS
#endif
namespace boost{
// HP aCC cannot deal with missing names for template value parameters
template <bool x> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true>{};
// HP aCC cannot deal with missing names for template value parameters
template<int x> struct static_assert_test{};
}
//
// Implicit instantiation requires that all member declarations be
// instantiated, but that the definitions are *not* instantiated.
//
// It's not particularly clear how this applies to enum's or typedefs;
// both are described as declarations [7.1.3] and [7.2] in the standard,
// however some compilers use "delayed evaluation" of one or more of
// these when implicitly instantiating templates. We use typedef declarations
// by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
// version gets better results from your compiler...
//
// Implementation:
// Both of these versions rely on sizeof(incomplete_type) generating an error
// message containing the name of the incomplete type. We use
// "STATIC_ASSERTION_FAILURE" as the type name here to generate
// an eye catching error message. The result of the sizeof expression is either
// used as an enum initialiser, or as a template argument depending which version
// is in use...
// Note that the argument to the assert is explicitly cast to bool using old-
// style casts: too many compilers currently have problems with static_cast
// when used inside integral constant expressions.
//
#if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS) && !defined(__MWERKS__)
#ifndef BOOST_MSVC
#define BOOST_STATIC_ASSERT( B ) \
typedef ::boost::static_assert_test<\
sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
#else
// __LINE__ macro broken when -ZI is used see Q199057
// fortunately MSVC ignores duplicate typedef's.
#define BOOST_STATIC_ASSERT( B ) \
typedef ::boost::static_assert_test<\
sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
> boost_static_assert_typedef_
#endif
#else
// alternative enum based implementation:
#define BOOST_STATIC_ASSERT( B ) \
enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
= sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
#endif
#endif // BOOST_STATIC_ASSERT_HPP
--
. 生命的意义在于 /\ ____\ /\_ \ /\_\ .
. 希望 \ \ \___/_\/\ \ \/_/__ __ _ _★ .
. 工作 \ \ ____\\ \ \ /\ \ /'__`\ /\`'_\ .
. 爱你的人 \ \ \___/ \ \ \___\ \ \/\ __//\ \ \/ .
. 和你爱的人 \ \___\ \ \_____\ \__\ \____\ \ \_\ .
. …… \/___/ \/_____/\/__/\/____/ \/_/ @126.com .
※ 来源:·BBS 水木清华站 bbs.edu.cn·[FROM: 202.114.32.225]
posted on 2007-12-14 10:02
金庆
阅读(304)
评论(1)
编辑
收藏
引用
所属分类:
1. C/C++
评论
#
re: boost::static_assert
2008-01-05 21:26
蚂蚁终结者
不错,虽然没用过。却值的学习
回复
更多评论
刷新评论列表
标题
姓名
主页
验证码
*
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关文章:
log4cxx配合boost::format
pthread_rwlock使用错误
STL容器的赋值运算符可以更灵活吗?
用bind来解耦
用const方法更改非mutable成员
"multiple definition of" 错误
VS2005编译libevent
Boost.Singals 教程
Boost.Python中文文档下载
修改Boost.date_time代码兼容VC6
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
Powered by:
C++博客
Copyright © 金庆