Search code examples
unsignedunsigned-integer

`using my_type = unsigned;` vs `using my_type = unsigned int;` in C++


In C++, are the following two code snippets equivalent?

    using my_type = unsigned;
    my_type variable;

and

    using my_type = unsigned int;
    my_type variable;

If they are, why bother defining my_type instead of using unsigned or even unsigned int? Considering the latter two are a bit longer, but more explicit, when would one define and use my_type, unsigned and/or unsigned int?


Solution

  • Yes they are identical. Using my_type can make the intention clearer. E.g. consider using celsius = int;

    To get even better type safety you can use the newtype pattern (Google it) but it is a bit tedious in C++.

    The choice of unsigned Vs unsigned int is just taste.