Квалификаторы const, static, restrict в индексе массива в параметре функции и их комбинации
Прочитал примеры на cppreference, но не понял, как в случае передачи в функцию массива фиксированной длины могут помочь оптимизировать выполнение указанные квалификаторы и из комбинации. Прошу дать комментарий. Примерно так (возможно, есть ещё комбинации, о которых я не знаю, но хотелось бы узнать):
// Первый
void f(int m, int n, float a[restrict m][n], float b[restrict m][n]);
void g12(int n, float (*p)[n])
{
f(10, n, p, p+10); // OK
f(20, n, p, p+10); // possibly undefined behavior (depending on what f does)
}
// Второй
void fadd(double a[static 10], const double b[static 10])
{
for (int i = 0; i < 10; i++)
{
if (a[i] < 0.0) return;
a[i] += b[i];
}
}
// a call to fadd may perform compile-time bounds checking
// and also permits optimizations such as prefetching 10 doubles
int main(void)
{
double a[10] = {0}, b[20] = {0};
fadd(a, b); // OK
double x[5] = {0};
fadd(x, b); // undefined behavior: array argument is too small
}
// Третий
void fadd(double a[static restrict 10],
const double b[static restrict 10])
{
for (int i = 0; i < 10; i++) // loop can be unrolled and reordered
{
if (a[i] < 0.0)
break;
a[i] += b[i];
}
}
// Четвертый
int f(const int a[20])
{
// in this function, a has type const int* (pointer to const int)
}
int g(const int a[const 20])
{
// in this function, a has type const int* const (const pointer to const int)
}
Источник: Stack Overflow на русском