I came across the following code:
auto x = new int[10][10];
Which compiles and runs correctly but I can't figure out what would be the type for defining x separately from the assignment.
When debugging the type shown is int(*)[10] for x but int (*) x[10]; (or any other combination I tried) is illegal.
So are there cases where auto can't be replaced by an explicit type...? (and is this such a case?)
The type of
xisint (*)[10]. There are different ways of figuring this out. The simplest is to just try assigning5toxand noticing what the error says:Or just use
static_assert:This means that if you want to explicitly create
x(without usingauto) then you can do it as:Now coming to the title, one example where
autocannot be directly replaced by explicitly writing a type is when dealing with unscoped unnamed enum as shown below: DemoSimilarly, when dealing with unnamed class. Demo.