Re: Testing for code that should not compile?
Victor Bazarov wrote:
francis_r wrote:
Writing some test cases today I wondered if it would be possible to
assert that something does not compile.
What for?
Broadly speaking, for negative tests. Most of my testing career was in C
and C++ compiler development, and you have to test whether the compiler
issues diagnostics for illegal code. Libraries, on the other hand,
generally don't have that sort of constraint, but there are situations
where it's appropriate to say that some piece of code involving library
classes is ill formed,and that requires a negative test.
For example, the templated constructor for TR1's shared_ptr takes a
pointer p to the managed resource. It requires that delete p; be well
formed. So you write a test case:
struct Base
{
};
struct Derived : Base
{
private:
~Derived() {}
};
shared_ptr<Base> sp(new Derived);
A conforming implementation must issue a diagnostic on that last line.
The usual way to check that is to put that code in its own file, compile
it, and look at what the compiler says.
That's not the same as the original question, which asked about whether
you could check that something "shouldn't compile," but it's what people
usually mean when they say that.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]