为什么此宏接受带有1个参数的模板而拒绝带有2个参数的模板? [英] Why this macro accepts a template with 1 parameter and refuses a template with 2 parameters?
问题描述
我正在使用CPPUNIT 1.12.1.
I'm working with CPPUNIT 1.12.1.
它定义了那些宏:
#define CPPUNIT_TEST_SUITE_ADD_TEST( test ) \
context.addTest( test )
#define CPPUNIT_TEST( testMethod ) \
CPPUNIT_TEST_SUITE_ADD_TEST( \
( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
context.getTestNameFor( #testMethod), \
&TestFixtureType::testMethod, \
context.makeFixture() ) ) )
我想使用模板将多个测试添加到同一测试套件中(随着CPPUNIT的工作,每个测试必须是一个void
函数,因此使用模板可以使用不同的参数"调用同一void
函数...).
I want to add many tests to the same test suite using templates (as CPPUNIT works, every test must be a void
function, so using template makes it possible to call the same void
function with different "parameters"...).
这很完美:
class MyTestSuite1 : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(MyTestSuite1);
CPPUNIT_TEST(doTest<false>);
CPPUNIT_TEST(doTest<true>);
CPPUNIT_TEST_SUITE_END();
template<bool param> void doTest() { /* test here */ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(MyTestSuite1);
而事实并非如此:
class MyTestSuite2 : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(MyTestSuite2);
CPPUNIT_TEST(doTest<false,false>);
CPPUNIT_TEST(doTest<true,false>);
CPPUNIT_TEST_SUITE_END();
template<bool param1,bool param2> void doTest() { /* test here */ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(MyTestSuite2);
编译器(Visual Studio 2015)报告:
Compiler (Visual Studio 2015) reports:
1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(20): 警告C4002:宏'CPPUNIT_TEST'的实际参数过多 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(21): 警告C4002:宏'CPPUNIT_TEST'的实际参数过多 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(20): 错误C2059:语法错误:')' 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(21): 错误C2059:语法错误:')' 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(22): 错误C2143:语法错误:缺少';'前 '}' 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(22): 错误C2065:名称":未声明的标识符 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(22): 错误C2065:工厂":未声明的标识符 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(22): 错误C2059:语法错误:')' 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(29): 错误C2143:语法错误:缺少';'前 '{' 1> b:\ dev \ vobs_diabeloop \ private \ tst \ regulation \ cppunit \ hyper_ftac3 \ test.cpp(30): 错误C2143:语法错误:缺少';'在"{"
1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(20): warning C4002: too many actual parameters for macro 'CPPUNIT_TEST' 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(21): warning C4002: too many actual parameters for macro 'CPPUNIT_TEST' 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(20): error C2059: syntax error: ')' 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(21): error C2059: syntax error: ')' 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(22): error C2143: syntax error: missing ';' before '}' 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(22): error C2065: 'namer': undeclared identifier 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(22): error C2065: 'factory': undeclared identifier 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(22): error C2059: syntax error: ')' 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(29): error C2143: syntax error: missing ';' before '{' 1>b:\dev\vobs_diabeloop\private\tst\regulation\cppunit\hyper_ftac3\test.cpp(30): error C2143: syntax error: missing ';' before '{'
那是为什么?宏如何正确处理1个模板参数,但失败两个?知道如何轻松地进行编译和工作吗?
Why is that? How could the macro handle correctly 1 template parameter, but fails for two? Any idea how I could easily have it compile and work?
已经尝试CPPUNIT_TEST((doTest<false,false>));
失败(获取error C2143: syntax error: missing ';' before ')'
)
Already tried CPPUNIT_TEST((doTest<false,false>));
without success (getting error C2143: syntax error: missing ';' before ')'
)
推荐答案
CPPUNIT_TEST(doTest<false,false>);
该命令不起作用,因为宏认为您正在传递2个宏参数:doTest<false
和false>
.
This one doesn't work because macro thinks you are passing 2 macro parameters: doTest<false
and false>
.
CPPUNIT_TEST((doTest<false,false>));
这不起作用,因为&TestFixtureType::testMethod
将扩展为&TestFixtureType::(doTest<false,false>)
,这是无效的.
This doesn't work because &TestFixtureType::testMethod
will expand to &TestFixtureType::(doTest<false,false>)
which is invalid.
正如Piotr在评论中提到的那样,您可以使用以下代码:
As mentioned by Piotr in comment, you can use this code:
#define COMMA ,
class MyTestSuite2 : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(MyTestSuite2);
CPPUNIT_TEST(doTest<false COMMA false>);
CPPUNIT_TEST(doTest<true COMMA false>);
CPPUNIT_TEST_SUITE_END();
template<bool param1, bool param2> void doTest() { /* test here */ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(MyTestSuite2);
因为预处理程序看到您要传递1个参数
Because pre-processor sees that you want to pass 1 parameter
这篇关于为什么此宏接受带有1个参数的模板而拒绝带有2个参数的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!