clang++ cannot initialize a variable of type 'int(*)[dim2]' with an rvalue of type 'int (*)[dim2]'

前端 未结 3 1429
南笙
南笙 2021-01-27 12:31

Why does the code

void fcn(int *twoDArrayPtr, const int dim1, const int dim2) {
    int (*array)[dim2] = reinterpret_cast(twoDArrayPtr);
}

         


        
3条回答
  •  难免孤独
    2021-01-27 12:48

    You're trying to use C99's Variable Length Array(VLA) feature when you use dim2 as the array dimension in your cast. (gcc, for example does support this by extension: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html.)

    Good news, you can't do this now but you will be able to soon with the introduction of C++14's Runtime Sized Arrays.

    Pertainant quotes:

    Runtime-sized arrays offer the same syntax and performance of C99’s VLAs... Bear in mind that runtime-sized arrays aren’t precisely the same as C99’s VLAs. The C++14 feature is more restrained, which is just as well. Specifically, the following properties are excluded:

    • Runtime-sized multidimensional arrays
    • Modifications to the function declarator syntax
    • sizeof(a) being a runtime-evaluated expression returning the size of a
    • typedef int a[n]; evaluating n and passing it through the typedef

    So you're code will be legal soon, circa C++14.

    I've tried it out on the Visual Studio 2015 Beta and sadly at time of writing it is not supported :(

提交回复
热议问题