8067306: Improve STATIC_ASSERT

New improved implementation

Reviewed-by: ehelin, stefank
This commit is contained in:
Kim Barrett 2015-01-09 11:33:48 -05:00
parent 24d3bb517b
commit 2a73208887

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -225,21 +225,22 @@ void report_untested(const char* file, int line, const char* message);
void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2); void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2);
#ifdef ASSERT // Compile-time asserts. Cond must be a compile-time constant expression that
// Compile-time asserts. // is convertible to bool. STATIC_ASSERT() can be used anywhere a declaration
template <bool> struct StaticAssert; // may appear.
template <> struct StaticAssert<true> {}; //
// Implementation Note: STATIC_ASSERT_FAILURE<true> provides a value member
// rather than type member that could be used directly in the typedef, because
// a type member would require conditional use of "typename", depending on
// whether Cond is dependent or not. The use of a value member leads to the
// use of an array type.
// Only StaticAssert<true> is defined, so if cond evaluates to false we get template<bool x> struct STATIC_ASSERT_FAILURE;
// a compile time exception when trying to use StaticAssert<false>. template<> struct STATIC_ASSERT_FAILURE<true> { enum { value = 1 }; };
#define STATIC_ASSERT(cond) \
do { \ #define STATIC_ASSERT(Cond) \
StaticAssert<(cond)> DUMMY_STATIC_ASSERT; \ typedef char STATIC_ASSERT_FAILURE_ ## __LINE__ [ \
(void)DUMMY_STATIC_ASSERT; /* ignore */ \ STATIC_ASSERT_FAILURE< (Cond) >::value ]
} while (false)
#else
#define STATIC_ASSERT(cond)
#endif
// out of shared space reporting // out of shared space reporting
enum SharedSpaceType { enum SharedSpaceType {