I am learning C++. And my professor uses some code which is something like
using filePath = std::string;
using setOfPaths = std::set;
using i
The using keyword is used to define type aliases. The reasons your professor is using it are:
typenameYou can use type aliases to semantically (and only that) restrict a specific type, making the name more descriptive for the specific use.
An example is:
using fileName = std::string;
The fileName alias is used to describe a file name string, not just any string. This makes for readable function signatures too.
I feel like I have to iterate this again: it's simply an alias. Any function taking fileName as an argument will work just fine with any std::string argument.
typenamesSome may seem unnecessary, like:
using setOfPaths = std::set<filePath>;
but in some cases they can be actually used to avoid having to specify typename in situations like:
template<typename Type>
struct something {
using something_iter = typename std::set<Type>::iterator;
};
with:
template<typename Container>
using itertype = typename Container::iterator;
template<typename Type>
struct something {
using something_iter = itertype<std::set<Type>>;
};
By moving typename in a specific alias we can reuse itertype in multiple other occasions effectively avoiding typename.
typedefThere's another way to define type aliases: typedef. That keyword is inherited from C and does not allow for templated aliases, like:
template<typename Type>
using vec = std::vector<Type>;
This is not actually any more type safe than not using aliases at all. Again, fileName and std::string are exactly the same type. You can use both interchangeably.
A possible next step would be to define a specific fileName class/struct type with its own specific invariants.