Comma as separator in variable initialization (not as operator)
Per [dcl.decl]/3
Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself. [...]
we get that
unsigned int n = foo.size, nxn = n * n;
is the same as
unsigned int n = foo.size;
unsigned int nxn = n * n;
There is a note with exceptions for other rules like auto
or if a name shadows the type but those don't apply in this case.
Be very wary with pointers if you put multiple variables on a single line
int * foo, bar;
does not give you two pointers. Instead, foo
is a pointer and bar
is an int
. You would need
int * foo, * bar;
to get two pointers. For this reason I would prefer to use
int * foo;
int * bar;
and pay the extra keystorkes for safeties sake.
nxn
will be initialized properly, since n
has been defined and initialized at the point where nxn
is defined.
For clarity however, it would be better to put the variables on separate lines. Doing so avoids ambiguity, making your intent more clear to anyone who reads your code.