C++ Alias Declaration

| 分类 C++  | 标签 C++_keywords  Effective_Modern_C++ 

Prefering alias declaration than typedef. It is much complicated to use typedef for template than the other. As the following code shows:

template<typename T>
using MyAllocList = std::list<T, MyAlloc<T> >; // MyAllocList<T>
// is synonym for
// std::list<T, MyAlloc<T> >
MyAllocList<Widget> lw; // client code

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;
// client code

In the above code both alias declaration and typedef works as the same thing.


上一篇     下一篇