There is a special optimisation dedicated especially for that. It can deduce the check is not needed at all by static code analysis, like e.g. in this code:
1 2 3
for (int i = 0; i < array.length; ++i) {
// do something with array[i]
}
If it cannot statically elide the check, because the number of loop turns is not known at compile time (e.g. the loop condition is too complex to statically analyze), it tries to move the checks out of the loop.
Okay, if I move the #include "ArrayIndexOutOfBoundsException.hpp" into global scope, I still get the same compiler error. ...?
I'm getting no errors on my Exception class not declaring it's constructors and methods with throw(). I know exception has this, but do I NEED to do this in a derived class for it to compile. I though throw() was just a compiler check (fool proofing.)
Where am I trying to copy an ostream? I don't see what you're referring too. (And there are no compiler errors about this one too.)
That stuff about Java's bounds checking optimization is interesting. I'm thinking about adding something like that to my Array class, of course the decision would be manual and not automatic; something like a template or C define macro (although I much more prefer the first.) This would come later though as two logical checks is not a huge deal, even if iterated over 100's of times. We're talking about ms at the most here.
//This is in Object.cpp
//operator definitions
ostream /*Here you are trying to copy an ostream*/
operator<<(ostream &out, const Object &obj) {
return out << obj.toString();
}
gcc complains
error: looser throw specifier for ‘virtual const char* mhc8::Exception::what() const’
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//This is in Array.cpp
#ifndef ...
#define ...
namespace mch8{
class Array{
//...
};
}
#include "AIOOBE.hpp"
namescape mch8{
//Definition of the methods of Array
}
My only current solution is to push class declarations/definitions Array, String, ArrayIndexOutOfBoundsException, and method definitions for Array all into the same file, and I don't want to do that.
Array is a template. I can't define Array_declaration and Array_implementation in different files (at least that's what I thought.)
I tried what you have above, it's not compiling. I'm using Visual Studios 2008 (cl.exe)
Object.cpp includes String.hpp
String.hpp includes Array.hpp
Array.hpp declares/defines Array.hpp
Array.hpp includes ArrayIndexOutOfBoundsException.hpp (... which includes Exception.hpp)
Exception.hpp tries to use String, but it hasn't been defined yet!
Yes, you can. Including a header is the same that copy-paste. (The usual trick to separate declaration of implementation is
1 2 3 4 5 6 7 8 9
//foo_declaration.hpp
#ifndef FOO
#define FOO
template <class T>
class foo{
//...
};
#include "foo_implementation.hpp" //for the compiler is as it was written here
#endif
Using the include outside the guards (in String.hpp) will do
Object.cpp includes String.hpp
String.hpp includes Array.hpp
a Array.hpp declares class Array (define MHC8_ARRAY)
Array.hpp includes ArrayIndexOutOfBoundsException.hpp (... which includes Exception.hpp)
Exception.hpp includes String.hpp
String.hpp includes Array.hpp
The header is "empty" because of the guards abDeclaration of class String
Declaration of class Exception/AIOOBE
Definition of the methods of class Array
The header is "empty" because of the guards b
Definition of Object methods
Perhaps it is more clear with the super-header, that fixes the order of declaration/definition. Note that in that case you musn't include the inner headers (String.hpp, Array.hpp, etc) directly
The project's on hold 'till I can come up with a better solution (and 'till I have some time, finals are comming up.) I tryed moving the include outside of the header guard and for the most part it works (I get some weird linking errors with cl.exe.) However, that's not a fix I'm really willing to accept. I just forsee problems with multipule inclusion should I ever need to move any other include directives in a similar way. There has to be a workaround for this, I can't be the only person to have run into this type of problem before.