Search code examples
c++c++11using

"using" keyword in C++


I am learning C++. And my professor uses some code which is something like

using filePath = std::string;
using setOfPaths = std::set<filePath>;
using iterOfSet = setOfPaths::iterator;
using listOfIter = std::list<iterOfSet>;
using iterList = listOfIter::iterator;
using fileName = std::string;
using mapOfFileName = std::map<fileName, listOfIter>;
using iterOfMap = mapOfFileName::iterator;

setOfPaths _setOfPaths;
mapOfFileName _mapOfFileName;
iterOfSet setIter;

I want to know why we are using the using keyword. Why can't we simply write

std::string filepath;

std::set<filepath> setOfPaths;
...
...

What is the benefit of having using keyword?


Solution

  • The using keyword is used to define type aliases. The reasons your professor is using it are:

    • readability
    • being more descriptive
    • avoid unnecessary typename

    Readability and descriptiveness

    You 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.

    Unnecessary typenames

    Some 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.

    A note on typedef

    There'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>;
    

    A note on type safety

    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.