次の大まかな署名を持つコードがあります。
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;
}
ほとんどのコンパイラは、pArray の代入を喜んで飲み込みますが、nElements の代入では窒息します。この矛盾は私を混乱させます、そして私は悟りたいです。
const 整数を割り当てることができないことを受け入れるのに問題はありませんが、const-pointer-to-const に対して期待どおりに機能するのはなぜですか?
迅速かつ安価な修正は、const 修飾子を削除することですが、ループ内のコードの多くがマクロ化されているため、微妙なバグが発生する可能性があります (私は一度それに噛まれました)。上記をどのように再構成して、一定の要素カウンターを許可しますか?