# HG changeset patch # User kbarrett # Date 1420821228 18000 # Node ID 23a93b2e118cf73a87c33fc99df6b50da39d80e9 # Parent 2268133fdd5f809b36702f812ca244cf1486c505 8067306: Improve STATIC_ASSERT Summary: New improved implementation Reviewed-by: ehelin, stefank diff -r 2268133fdd5f -r 23a93b2e118c hotspot/src/share/vm/utilities/debug.hpp --- a/hotspot/src/share/vm/utilities/debug.hpp Fri Jan 09 08:38:23 2015 +0100 +++ b/hotspot/src/share/vm/utilities/debug.hpp Fri Jan 09 11:33:48 2015 -0500 @@ -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. * * This code is free software; you can redistribute it and/or modify it @@ -225,21 +225,22 @@ void warning(const char* format, ...) ATTRIBUTE_PRINTF(1, 2); -#ifdef ASSERT -// Compile-time asserts. -template struct StaticAssert; -template <> struct StaticAssert {}; +// Compile-time asserts. Cond must be a compile-time constant expression that +// is convertible to bool. STATIC_ASSERT() can be used anywhere a declaration +// may appear. +// +// Implementation Note: STATIC_ASSERT_FAILURE 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 is defined, so if cond evaluates to false we get -// a compile time exception when trying to use StaticAssert. -#define STATIC_ASSERT(cond) \ - do { \ - StaticAssert<(cond)> DUMMY_STATIC_ASSERT; \ - (void)DUMMY_STATIC_ASSERT; /* ignore */ \ - } while (false) -#else -#define STATIC_ASSERT(cond) -#endif +template struct STATIC_ASSERT_FAILURE; +template<> struct STATIC_ASSERT_FAILURE { enum { value = 1 }; }; + +#define STATIC_ASSERT(Cond) \ + typedef char STATIC_ASSERT_FAILURE_ ## __LINE__ [ \ + STATIC_ASSERT_FAILURE< (Cond) >::value ] // out of shared space reporting enum SharedSpaceType {