How do I initialize a Type * pointer using type () syntax?
You can not. The T() syntax is defined in 5.2.3 / 1,2 (C ++ 03, a slightly different wording in C ++ 11 FDIS). In particular, the second paragraph says:
The expression T (), where T is a specifier of a simple type (7.1.5.2) for an object type without an array or a type (void cv-qualified) void, creates an rvalue of the specified type, which is initialized with the value (8.5);
This means that int() will create an rvalue of type int and value-initializes it. Now the problem is that int* not a simple type specifier, but rather a specified type specifier. The definition of a simple type specifier in a grammar:
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
A type name is defined as:
type-name: class-name enum-name typedef-name
This is what makes the proposed solutions work. Creating a typedef (either directly or through a template) creates a type name (third type), which can be used as a specifier of a simple type (first type).
David Rodríguez - dribeas Nov 09 '11 at 16:35 2011-11-09 16:35
source share