I have found two more cases where alignment causes issues during 32bit compilation on VS 2013.
I have constructed minimal test cases that can reproduce the issues in VS 2013, please fill in the macro for alignment on other compilers. TEST_ALIGNMENT should be set larger than the default alignment for the platform, 32 should do it for most platforms. For reference: Default alignment on 32bit VS2013 is 8, and on 64bit it is 16.
Regarding HeapAlignedFixtureTest below, the standard only guarantees that new (gtest-internal.h line 484: virtual Test* CreateTest() { return new TestClass; }) will return an address suitably aligned for the largest basic type on the platform. So this is not MSVC specific unfortunately, a custom allocator could fix this but it's an invasive change that would need to be used through out gtest. We work around this using overloaded new/delete in class scope that do aligned allocation. As for AlignedParamTest below we are using a dummy param that has a pointer to a heap allocated, aligned param object, it's is not ideal but it works.
#define alignas(X) __declspec(align(X))
#error "PLEASE_IMPLEMENT alignas for other compilers!"
#define TEST_ALIGNMENT 32
struct alignas(TEST_ALIGNMENT) AlignedParam {
class HeapAlignedFixtureTest : public ::testing::Test {
// Causes warning C4316: Object allocated on the heap may not be aligned 32
// This warning should be treated as an error.
TEST_F(HeapAlignedFixtureTest, AddressTest){
auto this_addr = reinterpret_cast<uintptr_t>(¶m);
ASSERT_EQ(0, this_addr % TEST_ALIGNMENT);
class AlignedParamTest : public ::testing::TestWithParam<AlignedParam> {
TEST_P(AlignedParamTest, AddressTest){
auto param_addr = reinterpret_cast<uintptr_t>(&GetParam());
ASSERT_EQ(0, param_addr % TEST_ALIGNMENT);
INSTANTIATE_TEST_CASE_P(XX, AlignedParamTest, ::testing::Values(AlignedParam()));
Compiler output:
1>d:\work\include\gtest/internal/gtest-param-util.h(433): error C2719: 'parameter': formal parameter with __declspec(align('32')) won't be aligned
1> d:\work\include\gtest/internal/gtest-param-util.h(446) : see reference to class template instantiation 'testing::internal::TestMetaFactoryBase<AlignedParam>' being compiled
1> test/test_string.cpp(19) : see reference to class template instantiation 'testing::internal::TestMetaFactory<AlignedParamTest_AddressTest_Test>' being compiled
1>d:\work\include\gtest/internal/gtest-param-util.h(452): error C2719: 'parameter': formal parameter with __declspec(align('32')) won't be aligned
1>d:\work\include\gtest/internal/gtest-param-util-generated.h(79): error C2719: 'v1': formal parameter with __declspec(align('32')) won't be aligned
1> test/test_string.cpp(24) : see reference to class template instantiation 'testing::internal::ValueArray1<AlignedParam>' being compiled
1>d:\work\include\gtest/internal/gtest-internal.h(484): warning C4316: 'HeapAlignedFixtureTest_AddressxTest_Test' : object allocated on the heap may not be aligned 32
1> d:\work\include\gtest/internal/gtest-internal.h(484) : while compiling class template member function 'testing::Test *testing::internal::TestFactoryImpl<HeapAlignedFixtureTest_AddressxTest_Test>::CreateTest(void)'
1> test/test_string.cpp(31) : see reference to class template instantiation 'testing::internal::TestFactoryImpl<HeapAlignedFixtureTest_AddressxTest_Test>' being compiled