dimanche 21 décembre 2014

Should I `#include` statements within `main(){}`?

I'm working on a CFD solver. An existing CFD solver that I've used a fair amount is OpenFOAM. OpenFOAM routinely, within it's solvers, has things like:



...
int main ()
{
...
// Simplified version, but similar effect
double time = 0.0;
double endTime = 10.0;
while (time < endTime)
{
...
#include "fileThatSolvesForVelocity.H"
#include "fileThatSolvesForPressure.H"
...
time += deltaT;
}
}


Here, the files that are included aren't really 'headers' in the usual sense, since they just contain statements to be copied into main(). However, since they're run repeatedly (perhaps even repeatedly within one loop) this reduces code-duplication, and also compartmentalizes sections in reasonably obviously named 'header' files.


IDEs typically don't like this style of inclusion - they have no idea where all the variables in the 'header' come from, and can't provide any meaningful interpretation of things.


Is this style 'bad', or just a convenient use of C++ include mechanisms? In particular, should I attempt to avoid it, perhaps through use of classed objects for each field? Is there a better way to do this (C++11 and Boost are both available, if needed)?


Aucun commentaire:

Enregistrer un commentaire