williamr@2
|
1 |
// (C) Copyright John Maddock 2000.
|
williamr@2
|
2 |
// Use, modification and distribution are subject to the
|
williamr@2
|
3 |
// Boost Software License, Version 1.0. (See accompanying file
|
williamr@2
|
4 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
5 |
|
williamr@2
|
6 |
// See http://www.boost.org/libs/static_assert for documentation.
|
williamr@2
|
7 |
|
williamr@2
|
8 |
/*
|
williamr@2
|
9 |
Revision history:
|
williamr@2
|
10 |
02 August 2000
|
williamr@2
|
11 |
Initial version.
|
williamr@2
|
12 |
*/
|
williamr@2
|
13 |
|
williamr@2
|
14 |
#ifndef BOOST_STATIC_ASSERT_HPP
|
williamr@2
|
15 |
#define BOOST_STATIC_ASSERT_HPP
|
williamr@2
|
16 |
|
williamr@2
|
17 |
#include <boost/config.hpp>
|
williamr@2
|
18 |
#include <boost/detail/workaround.hpp>
|
williamr@2
|
19 |
|
williamr@2
|
20 |
#ifdef __BORLANDC__
|
williamr@2
|
21 |
//
|
williamr@2
|
22 |
// workaround for buggy integral-constant expression support:
|
williamr@2
|
23 |
#define BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS
|
williamr@2
|
24 |
#endif
|
williamr@2
|
25 |
|
williamr@2
|
26 |
#if defined(__GNUC__) && (__GNUC__ == 3) && ((__GNUC_MINOR__ == 3) || (__GNUC_MINOR__ == 4))
|
williamr@2
|
27 |
// gcc 3.3 and 3.4 don't produce good error messages with the default version:
|
williamr@2
|
28 |
# define BOOST_SA_GCC_WORKAROUND
|
williamr@2
|
29 |
#endif
|
williamr@2
|
30 |
|
williamr@2
|
31 |
namespace boost{
|
williamr@2
|
32 |
|
williamr@2
|
33 |
// HP aCC cannot deal with missing names for template value parameters
|
williamr@2
|
34 |
template <bool x> struct STATIC_ASSERTION_FAILURE;
|
williamr@2
|
35 |
|
williamr@2
|
36 |
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
|
williamr@2
|
37 |
|
williamr@2
|
38 |
// HP aCC cannot deal with missing names for template value parameters
|
williamr@2
|
39 |
template<int x> struct static_assert_test{};
|
williamr@2
|
40 |
|
williamr@2
|
41 |
}
|
williamr@2
|
42 |
|
williamr@2
|
43 |
//
|
williamr@2
|
44 |
// Implicit instantiation requires that all member declarations be
|
williamr@2
|
45 |
// instantiated, but that the definitions are *not* instantiated.
|
williamr@2
|
46 |
//
|
williamr@2
|
47 |
// It's not particularly clear how this applies to enum's or typedefs;
|
williamr@2
|
48 |
// both are described as declarations [7.1.3] and [7.2] in the standard,
|
williamr@2
|
49 |
// however some compilers use "delayed evaluation" of one or more of
|
williamr@2
|
50 |
// these when implicitly instantiating templates. We use typedef declarations
|
williamr@2
|
51 |
// by default, but try defining BOOST_USE_ENUM_STATIC_ASSERT if the enum
|
williamr@2
|
52 |
// version gets better results from your compiler...
|
williamr@2
|
53 |
//
|
williamr@2
|
54 |
// Implementation:
|
williamr@2
|
55 |
// Both of these versions rely on sizeof(incomplete_type) generating an error
|
williamr@2
|
56 |
// message containing the name of the incomplete type. We use
|
williamr@2
|
57 |
// "STATIC_ASSERTION_FAILURE" as the type name here to generate
|
williamr@2
|
58 |
// an eye catching error message. The result of the sizeof expression is either
|
williamr@2
|
59 |
// used as an enum initialiser, or as a template argument depending which version
|
williamr@2
|
60 |
// is in use...
|
williamr@2
|
61 |
// Note that the argument to the assert is explicitly cast to bool using old-
|
williamr@2
|
62 |
// style casts: too many compilers currently have problems with static_cast
|
williamr@2
|
63 |
// when used inside integral constant expressions.
|
williamr@2
|
64 |
//
|
williamr@2
|
65 |
#if !defined(BOOST_BUGGY_INTEGRAL_CONSTANT_EXPRESSIONS)
|
williamr@2
|
66 |
|
williamr@2
|
67 |
#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
|
williamr@2
|
68 |
// __LINE__ macro broken when -ZI is used see Q199057
|
williamr@2
|
69 |
// fortunately MSVC ignores duplicate typedef's.
|
williamr@2
|
70 |
#define BOOST_STATIC_ASSERT( B ) \
|
williamr@2
|
71 |
typedef ::boost::static_assert_test<\
|
williamr@2
|
72 |
sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)\
|
williamr@2
|
73 |
> boost_static_assert_typedef_
|
williamr@2
|
74 |
#elif defined(BOOST_MSVC)
|
williamr@2
|
75 |
#define BOOST_STATIC_ASSERT( B ) \
|
williamr@2
|
76 |
typedef ::boost::static_assert_test<\
|
williamr@2
|
77 |
sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
|
williamr@2
|
78 |
BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
|
williamr@2
|
79 |
#elif defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)
|
williamr@2
|
80 |
// agurt 15/sep/02: a special care is needed to force Intel C++ issue an error
|
williamr@2
|
81 |
// instead of warning in case of failure
|
williamr@2
|
82 |
# define BOOST_STATIC_ASSERT( B ) \
|
williamr@2
|
83 |
typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
|
williamr@2
|
84 |
[ ::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >::value ]
|
williamr@2
|
85 |
#elif defined(__sgi)
|
williamr@2
|
86 |
// special version for SGI MIPSpro compiler
|
williamr@2
|
87 |
#define BOOST_STATIC_ASSERT( B ) \
|
williamr@2
|
88 |
BOOST_STATIC_CONSTANT(bool, \
|
williamr@2
|
89 |
BOOST_JOIN(boost_static_assert_test_, __LINE__) = ( B )); \
|
williamr@2
|
90 |
typedef ::boost::static_assert_test<\
|
williamr@2
|
91 |
sizeof(::boost::STATIC_ASSERTION_FAILURE< \
|
williamr@2
|
92 |
BOOST_JOIN(boost_static_assert_test_, __LINE__) >)>\
|
williamr@2
|
93 |
BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
|
williamr@2
|
94 |
#elif BOOST_WORKAROUND(__MWERKS__, <= 0x3003)
|
williamr@2
|
95 |
// special version for CodeWarrior <= 8.x
|
williamr@2
|
96 |
#define BOOST_STATIC_ASSERT( B ) \
|
williamr@2
|
97 |
BOOST_STATIC_CONSTANT(int, \
|
williamr@2
|
98 |
BOOST_JOIN(boost_static_assert_test_, __LINE__) = \
|
williamr@2
|
99 |
sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) )
|
williamr@2
|
100 |
#else
|
williamr@2
|
101 |
// generic version
|
williamr@2
|
102 |
#define BOOST_STATIC_ASSERT( B ) \
|
williamr@2
|
103 |
typedef ::boost::static_assert_test<\
|
williamr@2
|
104 |
sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
|
williamr@2
|
105 |
BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
|
williamr@2
|
106 |
#endif
|
williamr@2
|
107 |
|
williamr@2
|
108 |
#else
|
williamr@2
|
109 |
// alternative enum based implementation:
|
williamr@2
|
110 |
#define BOOST_STATIC_ASSERT( B ) \
|
williamr@2
|
111 |
enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
|
williamr@2
|
112 |
= sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) }
|
williamr@2
|
113 |
#endif
|
williamr@2
|
114 |
|
williamr@2
|
115 |
|
williamr@2
|
116 |
#endif // BOOST_STATIC_ASSERT_HPP
|
williamr@2
|
117 |
|
williamr@2
|
118 |
|