由于我似乎对这个问题很感兴趣,所以我为自己想出了一个答案,头文件基本上是这样的:
exceptionized_static_assert.h
#ifndef TEST__EXCEPTIONALIZE_STATIC_ASSERT_H
#define TEST__EXCEPTIONALIZE_STATIC_ASSERT_H
/* Conditionally compilable apparatus for replacing `static_assert`
with a runtime exception of type `exceptionalized_static_assert`
within (portions of) a test suite.
*/
#if TEST__EXCEPTIONALIZE_STATIC_ASSERT == 1
#include <string>
#include <stdexcept>
namespace test {
struct exceptionalized_static_assert : std::logic_error
{
exceptionalized_static_assert(char const *what)
: std::logic_error(what){};
virtual ~exceptionalized_static_assert() noexcept {}
};
template<bool Cond>
struct exceptionalize_static_assert;
template<>
struct exceptionalize_static_assert<true>
{
explicit exceptionalize_static_assert(char const * reason) {
(void)reason;
}
};
template<>
struct exceptionalize_static_assert<false>
{
explicit exceptionalize_static_assert(char const * reason) {
std::string s("static_assert would fail with reason: ");
s += reason;
throw exceptionalized_static_assert(s.c_str());
}
};
} // namespace test
// A macro redefinition of `static_assert`
#define static_assert(cond,gripe) \
struct _1_test \
: test::exceptionalize_static_assert<cond> \
{ _1_test() : \
test::exceptionalize_static_assert<cond>(gripe){}; \
}; \
_1_test _2_test
#endif // TEST__EXCEPTIONALIZE_STATIC_ASSERT == 1
#endif // EOF
此标头仅包含在测试套件中,然后它将使static_assert
只有在构建测试套件时才可见的宏重新定义可见
`-DTEST__EXCEPTIONALIZE_STATIC_ASSERT=1`
这个装置的使用可以用一个玩具模板库来勾勒出来:
我的模板.h
#ifndef MY_TEMPLATE_H
#define MY_TEMPLATE_H
#include <type_traits>
template<typename T>
struct my_template
{
static_assert(std::is_pod<T>::value,"T must be POD in my_template<T>");
explicit my_template(T const & t = T())
: _t(t){}
// ...
template<int U>
static int increase(int i) {
static_assert(U != 0,"I cannot be 0 in my_template<T>::increase<I>");
return i + U;
}
template<int U>
static constexpr int decrease(int i) {
static_assert(U != 0,"I cannot be 0 in my_template<T>::decrease<I>");
return i - U;
}
// ...
T _t;
// ...
};
#endif // EOF
试着想象一下代码足够大和复杂,以至于你不能随便调查它并挑选出static_assert
s 并让自己确信你知道它们为什么在那里并且它们实现了它们的设计目的。您信任回归测试。
这是一个玩具回归测试套件my_template.h
:
测试.cpp
#include "exceptionalized_static_assert.h"
#include "my_template.h"
#include <iostream>
template<typename T, int I>
struct a_test_template
{
a_test_template(){};
my_template<T> _specimen;
//...
bool pass = true;
};
template<typename T, int I>
struct another_test_template
{
another_test_template(int i) {
my_template<T> specimen;
auto j = specimen.template increase<I>(i);
//...
(void)j;
}
bool pass = true;
};
template<typename T, int I>
struct yet_another_test_template
{
yet_another_test_template(int i) {
my_template<T> specimen;
auto j = specimen.template decrease<I>(i);
//...
(void)j;
}
bool pass = true;
};
using namespace std;
int main()
{
unsigned tests = 0;
unsigned passes = 0;
cout << "Test: " << ++tests << endl;
a_test_template<int,0> t0;
passes += t0.pass;
cout << "Test: " << ++tests << endl;
another_test_template<int,1> t1(1);
passes += t1.pass;
cout << "Test: " << ++tests << endl;
yet_another_test_template<int,1> t2(1);
passes += t2.pass;
#if TEST__EXCEPTIONALIZE_STATIC_ASSERT == 1
try {
// Cannot instantiate my_template<T> with non-POD T
using type = a_test_template<int,0>;
cout << "Test: " << ++tests << endl;
a_test_template<type,0> specimen;
}
catch(test::exceptionalized_static_assert const & esa) {
++passes;
cout << esa.what() << endl;
}
try {
// Cannot call my_template<T>::increase<I> with I == 0
cout << "Test: " << ++tests << endl;
another_test_template<int,0>(1);
}
catch(test::exceptionalized_static_assert const & esa) {
++passes;
cout << esa.what() << endl;
}
try {
// Cannot call my_template<T>::decrease<I> with I == 0
cout << "Test: " << ++tests << endl;
yet_another_test_template<int,0>(1);
}
catch(test::exceptionalized_static_assert const & esa) {
++passes;
cout << esa.what() << endl;
}
#endif // TEST__EXCEPTIONALIZE_STATIC_ASSERT == 1
cout << "Passed " << passes << " out of " << tests << " tests" << endl;
cout << (passes == tests ? "*** Success :)" : "*** Failure :(") << endl;
return 0;
}
// EOF
您test.cpp
至少可以使用 gcc 6.1、clang 3.8 和 option
-std=c++14
或 VC++ 19.10.24631.0 和 option进行编译/std:c++latest
。首先这样做而不定义 TEST__EXCEPTIONALIZE_STATIC_ASSERT
(或定义它= 0)。然后运行,输出应该是:
Test: 1
Test: 2
Test: 3
Passed 3 out of 3 tests
*** Success :)
如果你然后重复,但编译-DTEST__EXCEPTIONALIZE_STATIC_ASSERT=1
,
Test: 1
Test: 2
Test: 3
Test: 4
static_assert would fail with reason: T must be POD in my_template<T>
Test: 5
static_assert would fail with reason: I cannot be 0 in my_template<T>::increase<I>
Test: 6
static_assert would fail with reason: I cannot be 0 in my_template<T>::decrease<I>
Passed 6 out of 6 tests
*** Success :)
try/catch
显然,在静态断言测试用例中对块进行重复编码是乏味的,但是在设置一个真实且受人尊敬的单元测试框架时,人们会期望它会打包异常测试设备以在您的视线之外生成这样的东西。例如,在 googletest 中,您可以编写以下代码:
TYPED_TEST(t_my_template,insist_non_zero_increase)
{
ASSERT_THROW(TypeParam::template increase<0>(1),
exceptionalized_static_assert);
}
现在我可以回到我对世界末日日期的计算 :)