5

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.

4
  • 2
    There is no typeof in c++. If you use compiler extensions - which you apparently do - then state which compiler you're asking about.
    – eerorika
    Commented Nov 13, 2015 at 11:24
  • I get error: expected type-specifier before ‘typeof’ - did you mean to write decltype? Commented Nov 13, 2015 at 11:39
  • @TobySpeight It's a gcc extension.
    – molbdnilo
    Commented Nov 13, 2015 at 12:18
  • 1
    @molbdnilo - ah, so -std=gnu++11 instead of -std=c++11; thanks. Commented Nov 13, 2015 at 12:46

2 Answers 2

3

or a standard C++11 feature, both don't appear to be working as per my expectations in all cases.

Then you have wrong expectations - at least in the case of decltype. I don't know how the gcc's typeof works, so I can't answer to why B) works but C) doesn't but D) and E) behave according to the standard.

D) The expression given to decltype in this example is a. a is name of a variable. In other words it is an id-expression. In the case of an id-expression, decltype resolves to the type of the entity. The type of a is int*. Therefore new decltype(a)[5]; constructs an array of int*. Because the type of a isn't int**, it cannot point to an array of int*.

E) The expression given to decltype in this example is *a. The type (T) of the expression is int. But *a isn't a name of a variable. It isn't an id-expression like a is. The value category of *a is lvalue. In case of an lvalue-expression, decltype resolves to T& where T is the type of the expression. Therefore, decltype(*a) is int&. Arrays of references are not allowed, therefore new decltype(*a)[5] is not allowed.

Here is the quote from the standard (draft):

§ 7.1.6.2 [dcl.type.simple]

  1. For an expression e , the type denoted by decltype(e) is defined as follows:

— if e is an unparenthesized id-expression or an unparenthesized class member access ( 5.2.5 ), decltype(e) is the type of the entity named by e . If there is no such entity, or if e names a set of overloaded func- tions, the program is ill-formed;

— otherwise, if e is an xvalue, decltype(e) is T&& , where T is the type of e ;

— otherwise, if e is an lvalue, decltype(e) is T& , where T is the type of e ;

— otherwise, decltype(e) is the type of e

This would be correct:

int * a = new std::remove_pointer<decltype(a)>::type[5];

and also this:

int * a = new std::remove_reference<decltype(*a)>::type[5];

Of course, both of those are quite pointlessly complicated. If your objective is to avoid repeating the type, then decltype is not the tool you should be looking for. The tool that fits your needs is auto:

auto a = new int[5];
1

*a is a pointer to int so cant call typeof on it this way

int *a = new int[5];

works because you are showing to integer and declarating the new array of size 5 on the place where a shows

maybe you wanted :

typeof(new typeof(int *)[5])a;

instead

 int * a = new typeof(*a)[5];
3
  • a is a pointer to int. *a is an int. typeof(*x) is even one of gcc's examples.
    – molbdnilo
    Commented Nov 13, 2015 at 12:16
  • *a is not a pointer to int. *a is what a points to, an int. So why doesn't it work ? I essentially want to eliminate any data type mention in the definition statement and want to use the existing variable itself, hence your suggestion typeof(new typeof(int *)[5])a; though correct, isn't what I am looking for.
    – Zoso
    Commented Nov 13, 2015 at 12:27
  • int *s; typeof (*s)m[5]; s=m;
    – Angen
    Commented Nov 13, 2015 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.