How do I value-initialize a Type* pointer using Type()-like syntax?
You cannot. The syntax T()
is defined in 5.2.3/1,2 (C++03, slightly different wording in C++11 FDIS). In particular the second paragraph states:
The expression T(), where T is a simple-type-specifier (7.1.5.2) for a non-array complete object type or the (possibly cv-qualified) void type, creates an rvalue of the specified type, which is value-initialized (8.5);
That means that int()
, will create an rvalue of type int and value-initialize it. Now the problem is that int*
is not a simple-type-specifier, but rather an elaborated-type-specifier. The definition of simple-type-specifier in the grammar is:
simple-type-specifier:
::opt nested-name-specifieropt type-name
::opt nested-name-specifier template template-id
char
wchar_t
bool
short
int
long
signed
unsigned
float
double
void
With type-name being defined as:
type-name:
class-name
enum-name
typedef-name
This is what makes the proposed solutions work. The creation of the typedef (either directly or through the template) creates a type-name (third type) that can be used as a simple-type-specifier (first type).
unsigned int
. – MicahmicawberT()
will work fine... I don't get how this question gets 11 upvotes :) – Chronon