typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UPtrMapSS; using UPtrMapSS = std::unique_ptr<std::unordered_map<std::string, std::string>>;
上面两种的效果是一样的。在处理函数指针的型别时,别名声明好像更容易理解:
1 2 3 4
// FP is a synonym for a pointer to a function taking an int and // a const std::string& and returning nothing typedefvoid(*FP)(int, const std::string&); // typedef same meaning as above using FP = void (*)(int, const std::string&); // alias declaration
别名声明可以模板化,这种情况下它们被称为别名模板,alias template
1 2 3 4
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