我正在尝试在Visual Studio 2017中编译最初在Linux上开发的C ++代码。该项目在Linux上编译,但在VS中编译。这些错误来自于尝试使用宏来比较模板参数。
呼叫:
EXPECT_MATRIX_EQUAL(filter.xhat, mat);
where' filter.xhat'和' mat'都是载体
定义:
#define EXPECT_MATRIX_EQUAL(...) \
NAVUTILS_TEST_MATRIX_EQUAL_(GTEST_NONFATAL_FAILURE_, __VA_ARGS__)
#define NAVUTILS_TEST_MATRIX_EQUAL_(fail, expected, actual, ...) \
NAVUTILS_TEST_EQUAL_(expected, \
actual, \
__matrices_equal(expected, actual, ##__VA_ARGS__), \
fail)
#define NAVUTILS_TEST_EQUAL_(expected, actual, testExpr, fail) \
GTEST_TEST_BOOLEAN_(testExpr, #testExpr, actual, expected, fail) \
<< "Value of " #expected " [" << (expected).rows() << " x " << (expected).cols() << "]:\n" \
<< (expected) \
<< "\n\nValue of " #actual " [" << (actual).rows() << " x " << (actual).cols() << "]:\n" \
<< (actual) \
<< "\n\n"
错误:
C:\Users\ANTadmin\Desktop\scorpion-cpp\test\include\LinearFilterTests.h(19): warning C4003: not enough actual parameters for macro 'NAVUTILS_TEST_MATRIX_EQUAL_'
C:\Users\ANTadmin\Desktop\scorpion-cpp\test\include\LinearFilterTests.h(19): error C2059: syntax error: ')'
C:\Users\ANTadmin\Desktop\scorpion-cpp\test\include\LinearFilterTests.h(19): error C2181: illegal else without matching if
C:\Users\ANTadmin\Desktop\scorpion-cpp\test\include\LinearFilterTests.h(19): error C2065: 'gtest_ar_': undeclared identifier
C:\Users\ANTadmin\Desktop\scorpion-cpp\test\include\LinearFilterTests.h(19): error C2228: left of '.c_str' must have class/struct/union
C:\Users\ANTadmin\Desktop\scorpion-cpp\test\include\LinearFilterTests.h(19): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'testing::internal::AssertHelper'
C:\Users\ANTadmin\Desktop\scorpion-cpp\test\include\LinearFilterTests.h(19): note: No constructor could take the source type, or constructor overload resolution was ambiguous
我假设MSVC必须有一些特殊的东西,我使用的宏是如何丢失的。根据我的研究,应该正确处理重载的宏,以便我不明白为什么我会收到这些错误。
答案 0 :(得分:1)
这里的主要问题是MSVC在宏扩展中的宏调用中处理__VA_ARGS__
的方式。请参阅this question以及答案,其中包含Microsoft关于标准解释的声明。正如在该问题的公认答案中所建议的那样,解决方案是一种额外的间接性:
#define EXPAND_(X) X
#define EXPECT_MATRIX_EQUAL(...) \
EXPAND_(NAVUTILS_TEST_MATRIX_EQUAL_(GTEST_NONFATAL_FAILURE_, __VA_ARGS__))
还有另一个问题:
__matrices_equal(expected, actual, ##__VA_ARGS__), \
如果##
为空,使用__VA_ARGS__
来避免包含前面的,是GCC扩展,不应该在可移植代码中使用。但是,显然它适用于MSVC。