A)This works:
int main() {
int * a = new int[5];
delete[] a;
return 0;
}
B)This errors out:
int main() {
int * a = new typeof(*a)[5];
delete[] a;
return 0;
}
with error: invalid types 'int[int]' for array subscript
C)This works:
int main() {
int * a = new typeof(a)[5];
delete[] a;
return 0;
}
I can't understand why B fails because after expansion the statement should look like this:
int *a = new int[5];
because typeof(*a)
is int.
Here's the experiment with decltype:
D) This doesn't work:
int main() {
int * a = new decltype(a)[5];
delete[] a;
return 0;
}
with error: cannot convert 'int**' to 'int*' in initialization. This is expected given that decltype(a) is int* so it translates to int *a = new int*[5];
which is incorrect.
E) But this doesn't work:
int main() {
int * a = new decltype(*a)[5];
delete[] a;
return 0;
}
with error: new cannot be applied to a reference type
So be it a GCC extension or a standard C++11 feature, both don't appear to be working as per my expectations in all cases.
typeof
in c++. If you use compiler extensions - which you apparently do - then state which compiler you're asking about.error: expected type-specifier before ‘typeof’
- did you mean to writedecltype
?-std=gnu++11
instead of-std=c++11
; thanks.