blob: 3f4344fbb71abe092e043fb6a07250cc6a9a0a59 [file] [log] [blame]
openvcdiff311c7142008-08-26 19:29:25 +00001// 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
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000021namespace open_vcdiff {
22
23// The VCD_COMPILE_ASSERT macro can be used to verify that a compile-time
openvcdiff311c7142008-08-26 19:29:25 +000024// expression is true. For example, you could use it to verify the
25// size of a static array:
26//
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000027// VCD_COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
28// content_type_names_incorrect_size);
openvcdiff311c7142008-08-26 19:29:25 +000029//
30// or to make sure a struct is smaller than a certain size:
31//
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000032// VCD_COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
openvcdiff311c7142008-08-26 19:29:25 +000033//
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000034// For the second argument to VCD_COMPILE_ASSERT, the programmer should supply
openvcdiff311c7142008-08-26 19:29:25 +000035// 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)
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000041// is what differentiates VCD_COMPILE_ASSERT from Boost static asserts.
openvcdiff311c7142008-08-26 19:29:25 +000042
43template <bool>
44struct CompileAssert {
45};
46
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000047} // namespace open_vcdiff
48
49#define VCD_COMPILE_ASSERT(expr, msg) \
50 typedef open_vcdiff::CompileAssert<static_cast<bool>(expr)> \
openvcdiff311c7142008-08-26 19:29:25 +000051 msg[static_cast<bool>(expr) ? 1 : -1]
52
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000053// Implementation details of VCD_COMPILE_ASSERT:
openvcdiff311c7142008-08-26 19:29:25 +000054//
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000055// - VCD_COMPILE_ASSERT works by defining an array type that has -1
openvcdiff311c7142008-08-26 19:29:25 +000056// elements (and thus is invalid) when the expression is false.
57//
58// - The simpler definition
59//
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000060// #define VCD_COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
openvcdiff311c7142008-08-26 19:29:25 +000061//
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;
openvcdiff@gmail.com732fff22010-08-04 18:00:00 +000068// VCD_COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
openvcdiff311c7142008-08-26 19:29:25 +000069// // 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_