SoFunction
Updated on 2025-04-07

Implementation of type alias in c++

Type alias, as the name implies, is to create a new name for a type, and use this new name is exactly the same as using the previous name.

Type alias for c++98 version

The type alias method of c++98 version uses the typedef keyword, and the type alias implemented through the typedef keyword. The following will show some type alias methods of c++98 version.

//Basic type aliastypedef int Integer;  //Don't forget the commatypedef unsigned int UInt; //Don't forget the comma
//Pointer type aliastypedef int* IntPtr;  //Don't forget the commatypedef char* CharPtr;  //Don't forget the comma
// Function pointer aliastypedef void (*FuncPtr)(int, int);  

//Complex type aliastypedef std::vector<std::string> StringVector;
typedef std::map<std::string, int> StringIntMap; 

Modern C++ type aliasing method

Using was introduced in the c++11 version, and type alias was implemented through using. The detailed example is as follows:

//The type alias of the basic typeusing Integer = int;
using UInt = unsigned int;

//Type alias of pointer typeusing IntPtr = int*;
using CharPtr = char*;

//Type alias of function pointerusing FuncPtr = void(*)(int, int);

// Type alias of complex typesusing StringVector = std::vector<std::string>;
using StringIntMap = std::map<std::string, int>;

Why introduce using?

The reason why using is introduced as the recommended type alias is that the use method has obvious advantages over typedef.

  • A clearer syntax
typedef void(*FuncPtr)(int, int);  //typedef formusing FuncPtr = void(*)(int, int);  //usingform,More intuitive
  • Template alias support

typedef does not support template alias, but using supports template alias. Please see the following example:

// Use using to create template aliastemplate<typename T>
using Vec = std::vector<T>;

// Use exampleVec&lt;int&gt; numbers;        // Equivalent to std::vector<int>Vec&lt;std::string&gt; words;  // Equivalent to std::vector<std::string>
// typedef cannot directly implement template alias, and requires additional wrappertemplate&lt;typename T&gt;
struct VectorTypedef {
    typedef std::vector&lt;T&gt; type;
};

Common Scenes

Simplify complex types

Many times when writing some complex types, every time you create a variable of the type or other scenarios using the type, you need to write a very long type name. Through type alias, we can define a short name. When using this type in the future, use this alias. Please see the following example:

//Simplify the stl containerusing StringSet = std::set&lt;std::string&gt;;
using IntMatrix = std::vector&lt;std::vector&lt;int&gt;&gt;;

// Simplify smart pointer typesusing StringPtr = std::shared_ptr&lt;std::string&gt;;
using IntUPtr = std::unique_ptr&lt;int&gt;;

Callback function type

using ErrorCallback = std::function<void(const std::string&)>;
using EventHandler = std::function<void(const Event&)>;

Use type alias in a class

class Container {
public:
    using value_type = int;
    using pointer = value_type*;
    using reference = value_type&amp;;
    
    // STL style type alias    using iterator = std::vector&lt;value_type&gt;::iterator;
    using const_iterator = std::vector&lt;value_type&gt;::const_iterator;
private:
    std::vector&lt;value_type&gt; data;
};

Things to note

Using is recommended in c++11 and later c++ versions, and typedef is not recommended.

This is the end of this article about the implementation of type alias for c++. For more related type alias content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!