I was trying to do something similar to this:
auto foo = int*(nullptr);
Which with VC++ doesn't compile with the error message:
Type name is not allowed
And with GCC 8.2 doesn't compile with:
Expected primary expression before 'int'
I was really curious as to why this appears to be an illegal syntax. In my mind it should be fine since literals can be initialized like this.
auto foo = int(2);
The only way I could think off to get this to work was to either make a type alias or do this:
auto foo = std::add_pointer_t<int>(nullptr);
I tried googling for this but frankly I don't even know how to properly formulate this question since my standardese is weak. Any insight would be appreciated!
int* is a "derived declarator type"(not a standard term, but useful for reasoning about this). A functional style cast notation (which is what
int(2) is) can only contain a "A simple-type-specifier or typename-specifier". A derived declarator type doesn't fall under either category.
You have to either:
write it out in the form of a C-style cast:
auto foo = (int*) nullptr;
or in the form of a C++-style cast:
auto foo = static_cast<int*>(nullptr);
or alias the pointer type:
using iptr = int*; auto foo = iptr(nullptr);