【发布时间】:2012-01-25 12:54:44
【问题描述】:
我想知道是否有可能编写一种单元测试来验证给定代码不能编译。
例如,我有一个模板类:
#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;
任何想法如何实现这种行为? (现在,我必须取消注释失败的代码并手动验证是否存在编译错误 - 我想避免这种情况)
【问题讨论】:
标签: c++ unit-testing templates testing compilation