C ++ testing compilation errors

I am a student and I am trying to write and run some test code for an assignment to test it before including it. What I'm trying to do now is to check that my code is preventing correct semantics.In my assignment, I declared for each of my classes its own private copy constructor and assignment operator, which are undefined and therefore do nothing. When they are called in my test program, I get compilation errors as I expected. Something like that:

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

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

Is there a way to use try / catch to get my test code to compile and run, but show me these errors happened? I 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. Aren't these "exceptions"? Will they not call the throw?

If I understand try / catch correctly, it handles runtime errors, not the errors I was getting above, right?

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

What's the easiest / best way to do what I'm trying to do?

I've looked at the documentation for Boost.Build and it's a little over my head. If I were to use it, how would I check that a file, say "test.cpp" is compiled, and possibly handle certain compilation errors that occur with "test.cpp"?

Thank you for your help!

PS This is one of my first posts, hopefully I did "enough" research and did everything else properly. Sorry if I didn't.

+3


source to share


4 answers


These are compiler errors, not exceptions. Exceptions are a mechanism for programmers to throw errors at runtime and catch / handle them. The compiler cannot even execute the executable to run because it recognizes that the code is incorrect and is invalid C ++ code.

If you want to make it a runtime error, make this method public / use friends / whatever you need to do to provide access to anything and throw an exception in the method definition, catch and handle the exception in the calling code.

I don't see any purpose in this. Always prefer compile-time error at runtime. Is always.



The C ++ standard defines what is valid or invalid code and some are left as undefined and other things are left up to those who implement the compiler. Any standard C ++ compiler will give an error because something doesn't conform to the standard / definition and is therefore invalid. Errors usually say that something is ambiguous or downright meaningless and you need to revisit what you wrote.

Runtime errors are either crashes or unexpected and unwanted actions from the user's point of view. Compiler errors are a compiler saying "I don't understand what you are saying. It doesn't make sense." Compiler warnings are a compiler saying "I'll let you do this, but I probably shouldn't. Are you really sure this is what you mean?"

+3


source


try-catch happens at runtime, whereas the compiler statically tries to bind the functions you call at compile time, so compilation will always fail.



Alternatively, if you want to use C ++ exceptions, you can simply implement copy and assignment methods, make them public, and just throw the exception in the body of those functions. Note that in any situation you should prefer static / compilation checks over runtime checks if you have a choice.

+3


source


What you really want to test is not a compiler failure, but you want to test certain assumptions about your class.

In the test file put #include <type_traits>

and then add

assert((std::is_assignable <myClass, myClass> ::value) == FALSE);
assert((std::is_copy_assignable<myClass> ::value) == FALSE);
assert((std::is_copy_constructible<myClass> ::value) == FALSE);

      

The following are the various signs you can check: http://en.cppreference.com/w/cpp/types

Please note, you will need to compile for C ++ 11 most of these features.

(as described above in Assert that the code does NOT compile )

+1


source


This type of compilation error cannot be suppressed. These are errors in terms of C ++ standards.

Of course, you can suppress some of these in your own (or patched) compiler.

0


source







All Articles