The typedef and using keywords in C++ are both used to create aliases for data types. However, there are some key differences between the two keywords.
typedef defines a new type, while using creates an alias for an existing type. This means that you can use a
typedef to declare variables, while you can only use a using to refer to a variable that has already been declared using a
typedef or a using.
typedef can be used to create aliases for any type, while
using can only be used to create aliases for user-defined types. This means that you cannot use a
using to create an alias for a built-in type, such as int or
float.
typedef declarations are hoisted, while
using declarations are not. This means that the
typedef declarations are evaluated at the top of the scope in which they are declared, while the
using declarations are evaluated at the point where they are encountered.
In general, you should use typedef if you want to define a new type that you will be using frequently. You should use
using if you want to refer to an existing type without having to write out the full name of the type every time you want to use it.
Here are some examples of how to use typedef and using:
C++
// Define a new type called `Point` that is a struct
typedef struct Point {
int x;
int y;
} Point;
// Declare a variable of type `Point`
Point point;
// Create an alias for the `Point` type called `MyPoint`
using MyPoint = Point;
// Declare a variable of type `MyPoint`
MyPoint my_point;
In both cases, the variable point and the variable my_point will be of type
Point. However, the first code defines a new type called Point, while the second code simply creates an alias for the existing type
struct Point.
The using declaration in the second code is not hoisted, which means that the
MyPoint alias is not evaluated until the point where it is encountered. This can lead to some unexpected behavior, so it is generally recommended to use
typedef instead of using if you want to define a new type.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The
typedefandusingkeywords in C++ are both used to create aliases for data types. However, there are some key differences between the two keywords.typedefdefines a new type, whileusingcreates an alias for an existing type. This means that you can use atypedefto declare variables, while you can only use ausingto refer to a variable that has already been declared using atypedefor ausing.typedefcan be used to create aliases for any type, whileusingcan only be used to create aliases for user-defined types. This means that you cannot use ausingto create an alias for a built-in type, such asintorfloat.typedefdeclarations are hoisted, whileusingdeclarations are not. This means that thetypedefdeclarations are evaluated at the top of the scope in which they are declared, while theusingdeclarations are evaluated at the point where they are encountered.In general, you should use
typedefif you want to define a new type that you will be using frequently. You should useusingif you want to refer to an existing type without having to write out the full name of the type every time you want to use it.Here are some examples of how to use
typedefandusing:C++
In both cases, the variable
pointand the variablemy_pointwill be of typePoint. However, the first code defines a new type calledPoint, while the second code simply creates an alias for the existing typestruct Point.The
usingdeclaration in the second code is not hoisted, which means that theMyPointalias is not evaluated until the point where it is encountered. This can lead to some unexpected behavior, so it is generally recommended to usetypedefinstead ofusingif you want to define a new type.