原文地址:
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是一个简单但常用的宏,顾名思义起到编译期
断言的功效,可以通过它,在编译时对开发环境以及类型定义进行检查。
此类型检测对程序运行时无任何效率和空间上的影响。
例如在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宏利用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的替代方案
// (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]
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]