have you ever used a container from STL in C++? LOL, I am kidding reading this article is transitive relation to your knowledge. the syntax of calling one container let's say a vector with pair of strings is a nightmare-even calling this line of code over a larage scale software, don't even start thinking of it. thanks to the c++ developer who invented a way for the lazy and redundant work.
std::vector<std::pair<std::string,std::string>>vec;
Typedef vs alias
typedef
std::unique_ptr<std::unordered_map<std::string, std::string>>
UPtrMapSS;
using UPtrMapSS =
std::unique_ptr<std::unordered_map<std::string, std::string>>;
in the above case it sounds not even a compelling reason to go for aliase. what if we called a function pointer using typedef and aliase.
// fp is a synonym for a pointer function taking an int
// and const std::string& and returning nothing
typedef void (*FP)(int, const std::string&); // typedef
// same meaning as above
using FP = void (*)(int, const std::string&); // alias
still not convinced? then let's go deeper and talk about the template. the scary talks.
Template with typedef and aliase
template<typename T>
using MyAllocList=std::list<T,MyAlloc<T>>;
MyAllocList lw; //client code
the above code used the alias which was introduced in C++ 11, but when it comes to typedef you have to make the cake from the scratch.
template<typename T> // MyAllocList<T>::type struct MyAllocList { // is synonym for
typedef std::list<T, MyAlloc<T>> type; // std::list<T, }; // MyAlloc<T>>
MyAllocList<Widget>::type lw;
it gets even worse when using the typedef with a template to create a list of types specified by a template parameter. you have to precede the typedef name with type name.
// using typedef
template<typename T>
class Widget { // Widget<T> contains
private: // a MyAllocList<T>
typename MyAllocList<T>::type list; // as a data member
... };
// using aliase vanshes the need of typename and ::type.
template<typename T>
using MyAllocList = std::list<T, MyAlloc<T>>; // as before
template<typename T>
class Widget {
private:
MyAllocList<T> list;
... };
Transforming from C++11 to C++14
it happens quite often you see the usage of the old version of the library in C++11 and not recommended in C++14 for instance fmax vs max, if happen and you had the electronic copy of implementing the new version of libraries then you will see the usage of the alias everywhere. see the below code represents most transformations and their func is to just as the comment don't go ahead it is a headache.
std::remove_const<T>::type // C++11: const T → T
std::remove_const_t<T> //C++14 equivalent
std::remove_reference<T>::type // C++11: T&/T&& → T
std::remove_reference_t<T> //C++14 equivalent
std::add_lvalue_reference<T>::type // C++11: T → T&
std::add_lvalue_reference_t<T> //C++14 equivalent
how the C++ converted the transformations above into aliases, oops sorry here I said it using an alias.
template <class T>
using remove_const_t = typename remove_const<T>::type;
template <class T>
using remove_reference_t = typename remove_reference<T>::type;
template <class T>
using add_lvalue_reference_t =
typename add_lvalue_reference<T>::type;
See? Couldn't be easier.
Things to Remember
• typedefs don’t support templatization, but alias declarations do.
• Alias templates avoid the “::type” suffix and, in templates, the “typename”prefix often required to refer to typedefs.
• C++14 offers alias templates for all the C++11 type traits transformations.