How C++ Using' or Alias-Declaration Is Better Than Typedef - Nextptr
How C++ Using' or Alias-Declaration Is Better Than Typedef - Nextptr
Overview
Defining type aliases or synonyms with typedef has always been an indispensable part of writing a good quality C++ code. To outline how
type aliases can help create more maintainable code, let's take an example of a simple banking application where a user can have multiple
accounts. The following code declares two account types: Checking and Savings. An STL map collection is used to store the list of user
accounts keyed by the int user identifier:
class CheckingAccount;
class SavingsAccount;
One problem with the above code is that the user identifier type is hardcoded to int. If the user identifier is changed to another type say
std::string, it would require making changes all over. Also sometimes, working with the expansive STL declarations could cause a nervous
system breakdown, e.g., when we have to declare them as function parameters:
We can make this code easier to maintain by defining a type alias or synonym for the user identifier. Moreover, we can simplify this code
even further by creating type aliases for STL collections:
// define maps
UserCheckingAccounts_t UserCheckingAccounts;
UserSavingsAccounts_t UserSavingsAccounts;
The above code with type synonyms is more intuitive and self-documenting. For a large part, the typedef declarations have worked well,
but they have limitations, notably when it comes to working with templates. In the next sections, we would see what those limitations are
and how they are overcome by the C++11 alias-declarations.
Alias-Declaration
Since C++11, the using statement can be used instead of typedef to define type synonyms. This new method of defining type aliases is
known as alias-declaration or type-alias. The synonyms of user identifier and account collection types with alias-declaration can be defined
as follows:
https://www.nextptr.com/tutorial/ta1193988140/how-cplusplus-using-or-aliasdeclaration-is-better-than-typedef 1/3
12/26/2019 How C++ ‘using’ or alias-declaration is better than typedef - nextptr
Note that, the alias-declaration definitions look like variable declarations, which improves the readability. The improvement-in-readability
argument is more persuasive when we compare the following function pointer alias definitions:
typedef void(*FP)(int);
// vs
using FP = void(*)(int);
Like function pointer declaration, another exotic and a rather lesser-known declaration is of reference to an array. With array references,
functions can take array parameters without decaying them to pointers. The below code shows an example of defining array-reference alias
and using that as a function parameter. Note that how the array reference parameter allows us to use a range-based loop:
// or with typedef
// typedef int(&IntArray4)[4];
Readability is required, but it might not be a compelling reason for some to ditch typedef in favor of alias-declaration. However, alias-
declarations have another feature, alias templates, which make them hard to overlook.
Alias Templates
Unlike the typedef, the alias-declarations can be templatized. With alias templates, we can define generic aliases and specialize them
further:
By introducing a generic type alias, UserAccounts_t, we have made it easier to modify the code for a different STL collection later. We can
change UserAccounts_t to another compatible collection without changing the code anywhere else:
To create UserAccounts_t generic alias with typedef, we would have to wrap typedef definition in a struct template:
template<typename A>
struct UserAccounts_t {
typedef std::map<UserId, std::vector<A>> type;
};
As we can see, the typedef version of the generic alias looks more like a hack than the cleaner alias template UserAccounts_t. Not only
that, the alias-templates are easier to use in templates as they do not require typename in front of them, whereas the struct wrapped typedef
must be preceded by typename. For more coverage on alias-templates and how they are better than the struct nested typedef, please refer
"Effective Modern C++" by Scott Meyers.
https://www.nextptr.com/tutorial/ta1193988140/how-cplusplus-using-or-aliasdeclaration-is-better-than-typedef 2/3
12/26/2019 How C++ ‘using’ or alias-declaration is better than typedef - nextptr
Conclusion
When it comes to defining simpler type aliases, choosing between typedef and alias-declaration could be a matter of personal choice.
However, while defining the more complex template aliases, function-pointer aliases, and array reference aliases, the alias-declaration is a
clear winner.
Further Reading
Type alias, alias template: cppreference
Linked Questions
Try these linked questions to test your knowledge of the covered subject.
Comments
Login to comment... GO
Related Tutorials
Trending Tags
JAVA C++ JAVASCRIPT C# C++11 JAVA-8 ECMASCRIPT-6 STRING ITERATOR ARRAY LAMBDA BOXING NULLABLE
BITWISE-OPERATIONS
2015 nextptr
https://www.nextptr.com/tutorial/ta1193988140/how-cplusplus-using-or-aliasdeclaration-is-better-than-typedef 3/3