Why are declarations in C and C++ so complicated? -
beginners astonished learn declaration
int* a, b; declares a pointer int , b int. stems curious way in declarations in c , c++ divided specifiers , declarators. declaration begins single list of specifiers, each entity declared has own declarator, independently modifies specifier in order give resulting type of entity. above declaration therefore has 2 declarators, *a , b. * in first declarator combines int specifier give type int* a, , second declarator has no modifiers, resulting type int.
at same time, in contexts type name expected (type-id in c++) combination of specifiers , optionally "abstract declarator", declarator omits name. int* example type name. type name not directly used in declarations. see above, int* a, b not interpreted type name followed 2 identifiers, 1 might expect.
so have specifiers, declarators, , combinations of 2 forming type names, type names aren't used declare variables of corresponding types, though literally names of types, defined lack name. what historical reasons grammar being way, complicated , counterintuitive? there benefit being able write code this?
int *a, (*b)();
Comments
Post a Comment