Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ testing for compile errors

I'm a student, and I'm trying to write and run some test code for an assignment to check it before I turn it in. What I'm trying to do now is test that my code prevents value semantics properly. In my assignment, I have declared for each of my classes its own private copy constructor and assignment operator that have no definition, and so do nothing. When they are called in my test program, I am getting compile errors like I expected. Something like this:

error: 'myClass::myClass(const &myClass)' is private'

error: 'myClass& myClass::operator=(const myClass&)' is private

Is there a way to use try/catch so that my test code will compile and run, but show me that these errors did occur? I've tried:

myClass obj1(...);
myClass obj2(...);
try{
  obj1 = obj2;
  throw 1;
}
catch(int e){
  assert(e==1);
}

but the compiler is still giving me the above errors. Are these not 'exceptions'? Will they not trigger a throw?

If I'm understanding try/catch correctly, it handles runtime errors, not the kind errors I was getting above, correct?

After doing some more research, it seems that there is no (easy) way of testing for certain compile errors natively within C++ (this maybe true for most languages, now that I think about it). I read a post that suggests writing some test code in a scripting language that attempts to compile snippets of C++ code and checks for any errors, and another post that recommends using Boost.Build.

What is the easiest/best way of doing what I'm trying to do?

I looked at the documentation for Boost.Build and it's a bit over my head. If I used it, how would I test that a file, say 'test.cpp' compiles, and maybe handle specific compile errors that occur with 'test.cpp'?

Thanks for your help!

P.S. This is one of my first posts, hopefully I've done "enough" research, and done everything else properly. Sorry if I didn't.

like image 346
Jared Roder Avatar asked Mar 29 '12 00:03

Jared Roder


People also ask

What are compile errors in C?

Compilation error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself.

What causes compilation error in C?

This can happen due to a multitude of reasons, the most common stems from missing characters that make the statement invalid, i.e., you may have a line of code that doesn't do anything due to a missing character, operator, or parentheses.

How do you know if error is compile or runtime?

A runtime error happens during the running of the program. A compiler error happens when you try to compile the code. If you are unable to compile your code, that is a compiler error. If you compile and run your code, but then it fails during execution, that is runtime.

Can syntax errors be detected by compiler?

All syntax errors and some of the semantic errors (the static semantic errors) are detected by the compiler, which generates a message indicating the type of error and the position in the Java source file where the error occurred (notice that the actual error could have occurred before the position signaled by the ...


1 Answers

These are compiler errors, not exceptions. Exceptions are a mechanism for programmers to throw run-time errors and catch/handle them. The compiler fails to even build an executable for you to run because it recognizes that the code is malformed and is invalid C++ code.

If you want to make this a run-time error, make the method public/use friends/whatever you need to do to provide access to something and throw an exception in the method's definition, the catch and handle the exception in the calling code.

I don't see a purpose in doing this however. Always prefer a compile-time error to a run-time error. Always.

The C++ standard defines what is valid or invalid code, with some things left as undefined and other things left up to whoever implements the compiler. Any standard compliant C++ compiler will give an error because something does not meet the standard/definition and is thus invalid. The errors are generally to say that something is ambiguous or straight up nonsensical and you need to revise what you've written.

Run-time errors are either crashes or behavior that is unintended and unwanted from the perspective of the user. Compiler errors are the compiler saying "I don't understand what you're saying. This doesn't make sense.". Compiler warnings are the compiler saying "I'll let you do this, but I probably shouldn't. Are you really sure this is what you meant?".

like image 79
Sion Sheevok Avatar answered Sep 19 '22 21:09

Sion Sheevok