ART: Use static_assert in down_cast
Use C++11 to write an actual compile-time assert.
Change-Id: I36bd94adbf6c732e103720308e1e6bf11065f474
diff --git a/runtime/base/casts.h b/runtime/base/casts.h
index be94c2e..138c2fd 100644
--- a/runtime/base/casts.h
+++ b/runtime/base/casts.h
@@ -19,6 +19,8 @@
#include <assert.h>
#include <string.h>
+#include <type_traits>
+
#include "base/macros.h"
namespace art {
@@ -65,16 +67,9 @@
template<typename To, typename From> // use like this: down_cast<T*>(foo);
inline To down_cast(From* f) { // so we only accept pointers
- // Ensures that To is a sub-type of From *. This test is here only
- // for compile-time type checking, and has no overhead in an
- // optimized build at run-time, as it will be optimized away
- // completely.
- if (false) {
- implicit_cast<From*, To>(0);
- }
+ static_assert(std::is_base_of<From, typename std::remove_pointer<To>::type>::value,
+ "down_cast unsafe as To is not a subtype of From");
- //
- // assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
return static_cast<To>(f);
}