Я заметил, что в gcc C11 вы можете передать любую матрицу функции fn(int row, int col, int array[row][col]). Как перевести мою приведенную ниже ссылку (в ссылку на другую команду stackoverflow) в C11 на программу в С++ 11?
C - выделение матрицы в функции
Как вы можете видеть, я могу перейти к функциям статических и динамических распределенных массивов в C11. Возможно ли это в С++ 11?
Я сделал примерную программу, основанную на разных ответах stackoverflow, но все функции работают для array1, и ни одна из них не работает для array2, где double array1[ROW][COL] = { { } } и auto array2 = new double[ROW][COL]()?
Как сделать функцию для обоих массивов, как это сделано в C11 fn(int row, int col, int array[row][col])?
#include <iostream>
#include <utility>
#include <type_traits>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
const int ROW=2;
const int COL=2;
template <size_t row, size_t col>
void process_2d_array_template(double (&array)[row][col])
{
    cout << __func__ << endl;
    for (size_t i = 0; i < row; ++i)
    {
        cout << i << ": ";
        for (size_t j = 0; j < col; ++j)
            cout << array[i][j] << '\t';
        cout << endl;
    }
}
void process_2d_array_pointer(double (*array)[ROW][COL])
{
    cout << __func__ << endl;
    for (size_t i = 0; i < ROW; ++i)
    {
        cout << i << ": ";
        for (size_t j = 0; j < COL; ++j)
            cout << (*array)[i][j] << '\t';
        cout << endl;
    }
}
// int array[][10] is just fancy notation for the same thing
void process_2d_array(double (*array)[COL], size_t row)
{
    cout << __func__ << endl;
    for (size_t i = 0; i < row; ++i)
    {
        cout << i << ": ";
        for (size_t j = 0; j < COL; ++j)
            cout << array[i][j] << '\t';
        cout << endl;
    }
}
// int *array[10] is just fancy notation for the same thing
void process_pointer_2_pointer(double **array, size_t row, size_t col)
{
    cout << __func__ << endl;
    for (size_t i = 0; i < row; ++i)
    {
        cout << i << ": ";
        for (size_t j = 0; j < col; ++j)
            cout << array[i][j] << '\t';
        cout << endl;
    }
}
int main()
{
    double array1[ROW][COL] = { { } };
    process_2d_array_template(array1);
    process_2d_array_pointer(&array1);    // <-- notice the unusual usage of addressof (&) operator on an array
    process_2d_array(array1, ROW);
    // works since a first dimension decays into a pointer thereby becoming int (*)[COL]
    double *b[ROW];  // surrogate
    for (size_t i = 0; i < ROW; ++i)
    {
        b[i] = array1[i];
    }
    process_pointer_2_pointer(b, ROW, COL);
    // allocate (with initialization by parentheses () )
    auto array2 = new double[ROW][COL]();
    // pollute the memory
    array2[0][0] = 2;
    array2[1][0] = 3;
    array2[0][1] = 4;
    array2[1][1] = 5;
    // show the memory is initialized
    for(int r = 0; r < ROW; r++)
    {
        for(int c = 0; c < COL; c++)
            cout << array2[r][c] << " ";
        cout << endl;
    }
    //process_2d_array_pointer(array2);
    //process_pointer_2_pointer(array2,2,2);
    int info;
    cout << abi::__cxa_demangle(typeid(array1).name(),0,0,&info) << endl;
    cout << abi::__cxa_demangle(typeid(array2).name(),0,0,&info) << endl;
    return 0;
}
