How to check that some code is not compiling in C ++?

Possible duplicate:
Unit test compile time error

I am wondering if it is possible to write a unit test type that will verify that the given code is not compiling .

For example, I have a template class:

#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); }; 

So, the test should complete successfully:

 struct bar_derived : bar_base {}; foo<bar_derived> f; 

but with an error:

 struct bar_other {}; foo<bar_other> f; 

Any ideas on how to achieve this behavior? (At the moment I have to uncomment the faulty code and manually check if there are compilation errors - I want to avoid this)

+4
source share
3 answers

Boost has compilation tests, and they do this by simply putting each of these tests in a single source file, and then try to compile each of them. Boost.Build supports special commands for running tests , which include if the file is compiled or not.

+3
source

The bottom line is that you will run the usual "should fail" unittest command, but instead of launching your compiled program, you start the compiler with an example that should fail.

For example, on gtest, this will be a "death test" in the compiler. http://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Death_Tests

0
source

In tests for code failure due to non-compilation, it makes little sense to use unit testing, even if you could.

The ability to unit test the code section does not compile, does not have its own value, since the code cannot be used in any case.

You may have an automated build process that runs unit tests after the test code is built without errors, but the fact that the compiler throws an error is a failure in itself.

To check the correctness of the code, module testers are used.

-7
source

Source: https://habr.com/ru/post/1392930/


All Articles