diff --git a/googletest/include/gtest/gtest-matchers.h b/googletest/include/gtest/gtest-matchers.h index 6b1bb6af30..1bb3140d72 100644 --- a/googletest/include/gtest/gtest-matchers.h +++ b/googletest/include/gtest/gtest-matchers.h @@ -421,7 +421,11 @@ class MatcherBase : private MatcherDescriberInterface { template ()> struct ValuePolicy { static const M& Get(const MatcherBase& m) { - return reinterpret_cast(m.buffer_); + // When inlined along with Init, need to be explicit to avoid violating + // strict aliasing rules. + const M *ptr = static_cast( + static_cast(&m.buffer_)); + return *ptr; } static void Init(MatcherBase& m, M impl) { ::new (static_cast(&m.buffer_)) M(impl);