原文地址:
http://www.smth.edu.cn/bbsanc.php?path=%2Fgroups%2Fdevelop.faq%2FProgramming%2Flanguages%2Fc_cpp%2Fboost%2FM.1021441404.O0
發(fā)信人: flier (小海 (:好日子不多了:)), 信區(qū): Programming
標(biāo) 題: boost::static_assert
發(fā)信站: BBS 水木清華站 (Wed May 15 13:43:35 2002)
BOOST_STATIC_ASSERT是一個(gè)簡單但常用的宏,顧名思義起到編譯期
斷言的功效,可以通過它,在編譯時(shí)對開發(fā)環(huán)境以及類型定義進(jìn)行檢查。
此類型檢測對程序運(yùn)行時(shí)無任何效率和空間上的影響。
例如在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。
也可以在模板函數(shù)中檢測入口參數(shù)的類型是否符合要求
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函數(shù)的輸入迭代子符合random access iterator的concept
此外還可以在模板類里面驗(yàn)證模板參數(shù)
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 */
};
通過以上示例,我們可以總結(jié)出BOOST_STATIC_ASSERT的基本使用規(guī)律
首先是全局上對編譯環(huán)境進(jìn)行檢測,看是否符合設(shè)計(jì)開發(fā)時(shí)的假定,這對程序的
可移植性有很大幫助。其次是在模板類、模板函數(shù)中,對作為模板參數(shù)的類型
進(jìn)行檢測,驗(yàn)證其是否符合設(shè)計(jì)時(shí)假設(shè)。最后是能夠?qū)p中的concept概念貫徹
到代碼中,使代碼強(qiáng)制性與設(shè)計(jì)同步,并增強(qiáng)代碼可讀性。
在實(shí)現(xiàn)上,BOOST_STATIC_ASSERT宏利用c++規(guī)范中,對不完整類型
即不可實(shí)例化的類型,在對其進(jìn)行sizeof運(yùn)算時(shí)編譯錯(cuò)誤的特性,完成功能
大概的簡化代碼如下
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__
注意為了能夠使用多個(gè)BOOST_STATIC_ASSERT,在類型命名時(shí)加入了
行號以區(qū)別。對namespace而言,因?yàn)橥籲amespace可能分布在多個(gè)不同的
頭文件中,而不同頭文件中可能在同一行使用BOOST_STATIC_ASSERT檢測,
所以必須用一個(gè)namespace把斷言檢測隔離開,如上面例子所示。
此外,對于VC來說,在使用/ZI參數(shù)時(shí),__LINE__宏會發(fā)生錯(cuò)誤,
(參見MSDN中Q199057錯(cuò)誤),好在VC會忽略typedef重復(fù)定義。
對某些不支持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
發(fā)信人: flier (小海 (:好日子不多了:)), 信區(qū): Programming
標(biāo) 題: boost::static_assert
發(fā)信站: BBS 水木清華站 (Wed May 15 13:43:35 2002)
boost::static_assert
BOOST_STATIC_ASSERT的使用
BOOST_STATIC_ASSERT是一個(gè)簡單但常用的宏,顧名思義起到編譯期
斷言的功效,可以通過它,在編譯時(shí)對開發(fā)環(huán)境以及類型定義進(jìn)行檢查。
此類型檢測對程序運(yùn)行時(shí)無任何效率和空間上的影響。
例如在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。
也可以在模板函數(shù)中檢測入口參數(shù)的類型是否符合要求
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函數(shù)的輸入迭代子符合random access iterator的concept
此外還可以在模板類里面驗(yàn)證模板參數(shù)
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 */
};
通過以上示例,我們可以總結(jié)出BOOST_STATIC_ASSERT的基本使用規(guī)律
首先是全局上對編譯環(huán)境進(jìn)行檢測,看是否符合設(shè)計(jì)開發(fā)時(shí)的假定,這對程序的
可移植性有很大幫助。其次是在模板類、模板函數(shù)中,對作為模板參數(shù)的類型
進(jìn)行檢測,驗(yàn)證其是否符合設(shè)計(jì)時(shí)假設(shè)。最后是能夠?qū)p中的concept概念貫徹
到代碼中,使代碼強(qiáng)制性與設(shè)計(jì)同步,并增強(qiáng)代碼可讀性。
BOOST_STATIC_ASSERT的實(shí)現(xiàn)
在實(shí)現(xiàn)上,BOOST_STATIC_ASSERT宏利用c++規(guī)范中,對不完整類型
即不可實(shí)例化的類型,在對其進(jìn)行sizeof運(yùn)算時(shí)編譯錯(cuò)誤的特性,完成功能
大概的簡化代碼如下
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__
注意為了能夠使用多個(gè)BOOST_STATIC_ASSERT,在類型命名時(shí)加入了
行號以區(qū)別。對namespace而言,因?yàn)橥籲amespace可能分布在多個(gè)不同的
頭文件中,而不同頭文件中可能在同一行使用BOOST_STATIC_ASSERT檢測,
所以必須用一個(gè)namespace把斷言檢測隔離開,如上面例子所示。
此外,對于VC來說,在使用/ZI參數(shù)時(shí),__LINE__宏會發(fā)生錯(cuò)誤,
(參見MSDN中Q199057錯(cuò)誤),好在VC會忽略typedef重復(fù)定義。
對某些不支持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]