1
我想建立自己的單元測試庫,我想建立測試用例如下:聲明指針的可變參數模板成員函數
template <typename... Args>
std::string concatenate(Args&&... args);
class my_test : public unit_test::test {
public:
my_test(int i, float f, double d) : i_(i), f_(f), d_(d) { }
void test1() { assert_true(i_ % 5 == 0, concatenate("i(", i_, ") not divisible by 5")); }
void test2() { assert_true(i_ > 0, concatenate("i(", i_, ") not greater than 0")); }
void test3() { assert_true(i_ % 2 == 0, concatenate("i(", i_, ") not divisible by 2")); }
private:
int i_;
float f_;
double d_;
};
int main()
{
unit_test::test_case<my_test,
&my_test::test1
&my_test::test2
&my_test::test3> my_test_case;
result r = my_test_case(1, 1.0f, 1.0);
}
爲了能夠定義test_case模板類,我需要到能夠聲明指針的可變參數模板成員函數:
class result {
unsigned int num_failures_;
unsigned int num_tests_;
};
template <typename Test, void(Test::*...MemFns)()>
class test_case;
不幸的是,克++ - 4.8和以上提供了以下錯誤:
main.cpp:137:52: error: template argument 3 is invalid
class test_case <Test, &Test::First, &Test::...Rest> {
^
main.cpp: In function 'int main(int, char**)':
main.cpp:194:28: error: template argument 2 is invalid
&my_test::test3>()(1, 1.0f, 1.0);
令人驚訝的是,g ++ - 4.7編譯並運行一些無效的代碼!
聲明指向成員函數的可變參數模板的正確方法是什麼?
Piotr,爲什麼g ++ - 4.7不會給'&Test :: ... Rest'語法錯誤?它如何能夠構建一些實際上是語法錯誤的東西? –