c++ - When I use typedef or using for an int, how much is it still an int? -
i tried following:
using idx = int; array<value, n> arr; for(idx = 0; < n; ++){ arr[i].dosomething(); }
i expected compiler issue warning or error when try use idx
if int
. not.
so, when use using
or typedef
alias type b, variables of type b still of type a, , vice versa? no type safety can achieved renaming type when looks same has different meaning.
(this related following question asked: how make types indexing)
an alias declaration or typedef
makes new name aliased type. idx
not different type int
in case; can used interchangeably no difference in semantics.
from [dcl.typedef] (emphasis mine):
a name declared typedef specifier becomes typedef-name. within scope of declaration, typedef-name syntactically equivalent keyword , names type associated identifier in way described in clause 8. a typedef-name synonym type. typedef-name not introduce new type way class declaration or enum declaration does.
Comments
Post a Comment