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// The COMPILE_ASSERT macro can be used to verify that a compile-time
22// expression is true. For example, you could use it to verify the
23// size of a static array:
24//
25//   COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
26//                  content_type_names_incorrect_size);
27//
28// or to make sure a struct is smaller than a certain size:
29//
30//   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
31//
32// For the second argument to COMPILE_ASSERT, the programmer should supply
33// a variable name that meets C++ naming rules, but that provides
34// a description of the compile-time rule that has been violated.
35// (In the example above, the name used is "foo_too_large".)
36// If the expression is false, most compilers will issue a warning/error
37// containing the name of the variable.
38// This refinement (adding a descriptive variable name argument)
39// is what differentiates COMPILE_ASSERT from Boost static asserts.
40
41template <bool>
42struct CompileAssert {
43};
44
45#define COMPILE_ASSERT(expr, msg) \
46  typedef CompileAssert<static_cast<bool>(expr)> \
47      msg[static_cast<bool>(expr) ? 1 : -1]
48
49// Implementation details of COMPILE_ASSERT:
50//
51// - COMPILE_ASSERT works by defining an array type that has -1
52//   elements (and thus is invalid) when the expression is false.
53//
54// - The simpler definition
55//
56//     #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
57//
58//   does not work, as gcc supports variable-length arrays whose sizes
59//   are determined at run-time (this is gcc's extension and not part
60//   of the C++ standard).  As a result, gcc fails to reject the
61//   following code with the simple definition:
62//
63//     int foo;
64//     COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
65//                               // not a compile-time constant.
66//
67// - By using the type CompileAssert<(static_cast<bool>(expr))>, we ensure that
68//   expr is a compile-time constant.  (Template arguments must be
69//   determined at compile-time.)
70//
71// - The array size is (static_cast<bool>(expr) ? 1 : -1), instead of simply
72//
73//     ((expr) ? 1 : -1).
74//
75//   This is to avoid running into a bug in MS VC 7.1, which
76//   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
77
78#endif  // OPEN_VCDIFF_COMPILE_ASSERT_H_
79