Метод первый: Const Copy
#define HorribleHackStart(Type, Name) \
Type HorribleHackTemp = Name; { const Type Name = HorribleHackTemp;
#define HorribleHackEnd \
}
int foo(void)
{
HorribleHackStart(int, x)
... Here x is an unchanging const copy of extern x.
... Changes made to x (by other code) will not ge visible.
HorribleHackEnd
}
Метод второй: указатель
int foo(void)
{
#define x (* (const int *) &x)
... Here x is effectively a const reference to extern x.
... Changes made to x (by other code) will be visible.
#undef x
}
Комментарии
Я бы не использовал ни один из них в рабочем коде, но ониможет быть полезно, если вы хотите скомпилировать код для проверки на предмет нарушения требования const для x внутри функции.