• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2008 Google Inc.
2 // Authors: Zhanyong Wan, Lincoln Smith
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef OPEN_VCDIFF_COMPILE_ASSERT_H_
17 #define OPEN_VCDIFF_COMPILE_ASSERT_H_
18 
19 #include <config.h>
20 
21 namespace open_vcdiff {
22 
23 // The VCD_COMPILE_ASSERT macro can be used to verify that a compile-time
24 // expression is true. For example, you could use it to verify the
25 // size of a static array:
26 //
27 //   VCD_COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
28 //                      content_type_names_incorrect_size);
29 //
30 // or to make sure a struct is smaller than a certain size:
31 //
32 //   VCD_COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
33 //
34 // For the second argument to VCD_COMPILE_ASSERT, the programmer should supply
35 // a variable name that meets C++ naming rules, but that provides
36 // a description of the compile-time rule that has been violated.
37 // (In the example above, the name used is "foo_too_large".)
38 // If the expression is false, most compilers will issue a warning/error
39 // containing the name of the variable.
40 // This refinement (adding a descriptive variable name argument)
41 // is what differentiates VCD_COMPILE_ASSERT from Boost static asserts.
42 
43 template <bool>
44 struct CompileAssert {
45 };
46 
47 }  // namespace open_vcdiff
48 
49 #define VCD_COMPILE_ASSERT(expr, msg) \
50   typedef open_vcdiff::CompileAssert<static_cast<bool>(expr)> \
51       msg[static_cast<bool>(expr) ? 1 : -1]
52 
53 // Implementation details of VCD_COMPILE_ASSERT:
54 //
55 // - VCD_COMPILE_ASSERT works by defining an array type that has -1
56 //   elements (and thus is invalid) when the expression is false.
57 //
58 // - The simpler definition
59 //
60 //     #define VCD_COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
61 //
62 //   does not work, as gcc supports variable-length arrays whose sizes
63 //   are determined at run-time (this is gcc's extension and not part
64 //   of the C++ standard).  As a result, gcc fails to reject the
65 //   following code with the simple definition:
66 //
67 //     int foo;
68 //     VCD_COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
69 //                               // not a compile-time constant.
70 //
71 // - By using the type CompileAssert<(static_cast<bool>(expr))>, we ensure that
72 //   expr is a compile-time constant.  (Template arguments must be
73 //   determined at compile-time.)
74 //
75 // - The array size is (static_cast<bool>(expr) ? 1 : -1), instead of simply
76 //
77 //     ((expr) ? 1 : -1).
78 //
79 //   This is to avoid running into a bug in MS VC 7.1, which
80 //   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
81 
82 #endif  // OPEN_VCDIFF_COMPILE_ASSERT_H_
83