I have a function which gets a pointer to a C-style array. When I explicitly set the type of the variable twoDArray
and then compile the code with Clang I get the following error:
main.cpp:8: error: variable-sized object may not be initialized
const T (&twoDArray)[dim1][dim2]{*reinterpret_cast<const T (*)[dim1][dim2]>(cVector)};
When I comment out the line and use the commented out part the code will compile correctly. When I use g++ to compile the code both variants will be accepted.
Is this an error in the Clang compiler, or does g++ accept some code in this case which is non standard?
#include <iostream>
using namespace std;
void printVector(const double *cVector, const size_t dim1, const size_t dim2)
{
const double (&threeDArray)[dim1][dim2]{*reinterpret_cast<const double (*)[dim1][dim2]>(cVector)};
// auto threeDArray{*reinterpret_cast<const double (*)[dim1][dim2]>(cVector)};
}
int main()
{
const size_t VEC_SIZE{2};
double cVector[VEC_SIZE][VEC_SIZE]{{1.1, 2.2}, {3.3, 4.4}};
size_t vecSize{VEC_SIZE};
printVector(&cVector[0][0], vecSize, vecSize);
return 0;
}