可能的重复:
单元测试编译时错误 https://stackoverflow.com/questions/605915/unit-test-compile-time-error
我想知道是否可以编写一种单元测试来验证给定的代码doesn't编译。
例如,我有一个模板类:
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_base_of.hpp>
struct bar_base {};
template <typename T>
class foo
{
BOOST_STATIC_ASSERT(::boost::is_base_of<T, bar_base>::value);
};
因此,测试应该成功:
struct bar_derived : bar_base {};
foo<bar_derived> f;
但应该失败:
struct bar_other {};
foo<bar_other> f;
有什么想法如何实现这种行为? (现在,我必须取消注释失败的代码并手动验证是否存在编译错误 - 我想避免这种情况)
Boost 确实有编译测试,他们通过简单地将每个测试放入一个源文件中,然后尝试编译每个测试来实现这一点。增强构建 http://www.boost.org/boost-build2/支持运行测试的特殊命令 http://www.boost.org/boost-build2/doc/html/bbv2/builtins/testing.html,其中包括测试文件是否编译。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)