Clang ++ cannot initialize a variable of type 'int (*) [dim2]' with rvalues โโof type 'int (*) [dim2]'
Why the code
void fcn(int *twoDArrayPtr, const int dim1, const int dim2) {
int (*array)[dim2] = reinterpret_cast<int (*)[dim2]>(twoDArrayPtr);
}
int main() {
return 0;
}
generate a compiler error
error: cannot initialize a variable of type 'int (*)[dim2]' with
an rvalue of type 'int (*)[dim2]'
The types are the same, so I think the assignment can be fulfilled. Since int (*)[dim2]
is a pointer to an array of size dim2
and as such can be a pointer to a bunch of arrays of size dim2
in contiguous pointer-indexed memory, I think this should work.
I am using clang ++ on Mac OS / X with the following version information:
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.0.0 Thread model: posix
source to share
dim2
is not a compile-time constant and VLAs (variable length arrays) do not exist in C ++. Some other compilers (like gcc) have language extensions that allow VLA in C ++, but clang's behavior is standard.
You can solve the problem with a class (or class template) that does address translation for you, like
// This is very rudimentary, but it a point to start.
template<typename T>
class array2d_ref {
public:
array2d_ref(T *p, std::size_t dim) : data_(p), dim_(dim) { }
T *operator[](std::size_t i) { return &data_[i * dim_]; }
private:
T *data_;
std::size_t dim_;
};
...
array2d_ref<int> array(twoDArrayPtr, dim2);
But I'm afraid that it is impossible (portable) to have a pointer to an array unless you know the dimension of the array at compile time.
source to share
You are trying to use the C99 Variable Length Array (VLA) function when using dim2
as an array dimension in your application. (gcc, for example, supports this extension: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html .)
The good news is, you can't do it now, but you will soon be able to with the introduction of C ++ 14 Runtime Sized Arrays .
Quotes: [/ p>
Runtime size arrays provide the same syntax and performance of VLA C99s ... Be aware that runtime size arrays are exactly the same as C99s VLAs. The C ++ 14 feature is more discreet, which is fine too. In particular, the following properties are excluded:
- Multidimensional arrays of runtime size
- Function Declarator Syntax Modifications
sizeof(a)
- expression, evaluated at runtime, returning sizetypedef int a[n];
evaluatingn
and passing it throughtypedef
So, soon you'll be legal, around C ++ 14.
I tried this on Visual Studio 2015 Beta and unfortunately not supported at the time of writing :(
source to share
Although clang does not support variable length arrays, there is a workaround. The following compilations with clang ++ 4.0.0:
void fcn(int *twoDArrayPtr, const int dim1, const int dim2) {
using array_type = int (*)[dim2];
array_type array = reinterpret_cast<array_type>(twoDArrayPtr);
}
int main() {
return 0;
}
I'm not sure why this alias declaration should matter. This certainly seems inconsistent.
source to share