C++ 如何测试某些代码不';t在C+中编译+;?

C++ 如何测试某些代码不';t在C+中编译+;?,c++,unit-testing,templates,testing,compilation,C++,Unit Testing,Templates,Testing,Compilation,可能重复: 我想知道是否有可能编写一种单元测试来验证给定的代码是否不能编译 例如,我有一个模板类: #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&l

可能重复:

我想知道是否有可能编写一种单元测试来验证给定的代码是否不能编译

例如,我有一个模板类:

#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);
};
#包括
#包括
结构bar_base{};
模板
福班
{
BOOST\u STATIC\u ASSERT(::BOOST::is\u base\u of::value);
};
因此,测试应在以下方面取得成功:

struct bar_derived : bar_base {};
foo<bar_derived> f;
struct bar_派生:bar_base{};
福福;
但应以以下方式失败:

struct bar_other {};
foo<bar_other> f;
struct bar_other{};
福福;

有没有办法实现这样的行为?(目前,我必须取消对失败代码的注释,并手动验证是否存在编译错误-我希望避免这种情况)

Boost确实有编译测试,他们只需将这些测试中的每一个放在一个源文件中,然后试着编译它们。支持,包括测试文件是否编译。

其要点是,您将运行一个正常的“应失败”单元测试,但不是运行编译后的程序,而是在应失败的示例上运行编译器


例如,在gtest上,这将是编译器上的“死亡测试”

怎么做?链接没有提到这一点。