1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
12 * Macros and template metaprogramming to implement compile- and run-time
19 #include <cml/core/cml_meta.h>
23 /* Join preprocessor macros into a new preprocessor macro: */
24 #define CML_JOIN(X,Y) CML_DO_JOIN(X,Y)
25 #define CML_DO_JOIN(X,Y) CML_DO_JOIN2(X,Y)
26 #define CML_DO_JOIN2(X,Y) X##Y
28 /* Change a macro value into a string: */
29 #define TO_STRING(X) TO_STRING2(X)
30 #define TO_STRING2(X) #X
32 /** Default undefined compile-time assertion struct. */
33 template<bool T
> struct STATIC_ASSERTION_FAILURE
;
35 /** Struct instantiated when a true assertion is made at compile-time. */
36 template<> struct STATIC_ASSERTION_FAILURE
<true> {
37 typedef true_type result
;
38 enum { value
= true };
41 /** Create a compile-time assertion.
43 * @note Compile-time assertions must be expressions that can be evaluated at
44 * comile time. This means that the expression must only rely on constants,
45 * enums, and/or template parameters, not variables having run-time storage
48 * @warning Enclose expressions that have commas with parens, otherwise the
49 * preprocessor will parse the commas as macro argument separators!
51 * @sa STATIC_ASSERTION_FAILURE
53 #define CML_STATIC_REQUIRE(_E_) \
54 typedef typename STATIC_ASSERTION_FAILURE<(_E_)>::result \
55 CML_JOIN(__cml_assert_test_typedef_, __LINE__)
58 /** A more meaningful compile-time assertion struct.
60 * The parameter M is a struct type which has been declared but not
61 * defined; e.g. struct this_is_an_error.
63 * When used with CML_STATIC_REQUIRE_M(<expr>,M), the compiler errors will
64 * contain the struct name at the point of the error.
66 template<bool T
, typename M
> struct STATIC_ASSERTION_FAILURE_M
{
67 typename
M::bogus result
;
70 /** Instantiated for true assertions. */
71 template<typename M
> struct STATIC_ASSERTION_FAILURE_M
<true,M
> {
72 typedef true_type result
;
73 enum { value
= true };
76 /** Create a compile-time assertion with a message.
78 * @note Compile-time assertions must be expressions that can be evaluated at
79 * comile time. This means that the expression must only rely on constants,
80 * enums, and/or template parameters, not variables having run-time storage
83 * @warning Enclose expressions that have commas with parens, otherwise the
84 * preprocessor will parse the commas as macro argument separators!
86 * @sa STATIC_ASSERTION_FAILURE_M
88 #define CML_STATIC_REQUIRE_M(_E_, _M_) \
89 typedef typename STATIC_ASSERTION_FAILURE_M<(_E_),_M_> \
90 ::result CML_JOIN(__bogus_assert_type_, __LINE__)
96 // -------------------------------------------------------------------------