I tried the following:
using Idx = int;
array<Value, N> arr;
for(Idx i = 0; i < N; i ++){
arr[i].doSomething();
}
I expected that the compiler would issue a warning or an error when I try to use Idx
as if it was an int
. But it does not.
So, when I use using
or typedef
to alias type A as a B, are variables of type B still also of type A, and vice versa? So that no type safety can be achieved by renaming a type when it looks the same but has a different meaning.
(This is related to the following question I recently asked: How to make types for indexing)
An alias declaration or typedef
just makes a new name for the aliased type. Idx
is not a different type from int
in this case; they can be used interchangeably with no difference in semantics.
From [dcl.typedef] (emphasis mine):
A name declared with the typedef specifier becomes a typedef-name. Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword and names the type associated with the identifier in the way described in Clause 8. A typedef-name is thus a synonym for another type. A typedef-name does not introduce a new type the way a class declaration or enum declaration does.