I have a piece of code with the following rough signature:
void evaluate(object * this)
{
static const int briefList[] = { CONSTANT_A, CONSTANT_Z };
static const int fullList[] = { CONSTANT_A, CONSTANT_B, ..., CONSTANT_Z};
const int const * pArray;
const int nElements;
int i;
if ( this->needDeepsEvaluation )
{
pArray = fullList;
nElements = sizeof(fullList) / sizeof(fullList[0]);
}
else
{
pArray = briefList;
nElements = sizeof(briefList) / sizeof(briefList[0]);
}
for ( i = nElements; i; i-- )
{
/* A thousand lines of optimized code */
}
this->needsDeepEvaluation = 0;
}
Most compilers will happily swallow the assignment of pArray, but chokes on the assignments of nElements. This inconsistency confuses me, and I would like to be enlightened.
I have no problem accepting that you can't assign a const integer, but then why does it works as I expect for the const-pointer-to-const?
The fast and cheap fix is to drop the const qualifier, but that might introduce subtle bugs since much of the code inside the loop is macrofied (I've been bitten by that once). How would you restructure the above to allow a constant element counter?