C++ using vs typedef | Difference Comparision

In this “vs” tutorial we will cover the differences between using and typedef in C++.

The using keyword and typedef are both used for alias-declaration, which means to create an alternate name for a datatype or namespace. There are some differences however, which may require you to have to pick one over the other.

Let’s discuss these differences point by point.

(We’ll have some examples on using and typedef at the end of the article, so be sure to take a look)


Compatibility

There are two points that must be discussed about compatibility.

First of all, if you have a C++ version prior to C++11, then you cannot use the using keyword. Hence, if you are maintaining legacy code, typedef would be the way to go.

Secondly, if you wish your program to be compatible with C, or to be built off C in some way, then you will want to use typedef as well. This is of course, because typedef is a C feature, and using is from C++.


Creating an alias for templates

The main reason behind why using is recommended over typedef (for C++) is when we need to create an alias for templates.

If we use the using keyword, then we have the following example.

template <typename T, int Width, int Length>
class Shape { };

template <typename T, int Side >
using Square = Shape<T, Side, Side>;

int main() {
    Shape<int, 5, 5> square1;
    Square<int, 5> square2;
}

And if we use the typedef keyword.

template <typename T, int Width, int Length>
class Shape { };

template <typename T, int Side >
struct Square {
    typedef Shape<T, Side, Side> type;
};

int main() {
    Shape<int, 5, 5> square1;
    Square<int, 5>::type square2;
}

The point of alias-declaration is to show that we can create an object of the same type (Shape), but using a different name. Shape<int, 5, 5> and Square<int, 5> do the same thing, but the latter is a bit easier to write.

Both it’s name, and the fact that we were able to reduce one parameter required, makes using it repeatedly easier.


Readability

A rather minor point, but most people are of the opinion that using is easier to write and read then typedef. This may vary off previous experience of the user, and his own personal preferences.


Examples on using and typedef

Here are a few examples on the usage of typedef and using.

A very common usage of using is when we declare namespaces in C++.

using namespace std;

The above statement is not recommended by some, as it tends to pollute the namespace, so we can also narrow our range down to specific Classes

using std::string;

The above statement allows us to use string without having to include the std:: infront of it, but everything else in std library will require it.

A common usage of typedef, that is used in C to avoid having to write struct Vector every time a variable is created.

struct Vector {
  int x;
  int y
};

typedef struct Vector Vector;

int main() {
    Vector v1 = {4, 5};

    return 0;
}

This is a complete of alias for templates that we discussed earlier, using the using keyword.

#include <iostream>
using namespace std;

template <typename T, int Width, int Length>
class Shape {
public:
    int w, l;

    Shape() {
        w = Width;
        l = Length;
    }
};

template <typename T, int Side >
using Square = Shape<T, Side, Side>;


int main() {
    Shape<int, 5, 5> square1;
    Square<int, 5> square2;

    cout << square1.w << " " << square1.l << endl;
    cout << square2.w << " " << square2.l << endl;
}

5 5
5 5

This marks the end of the Using vs Typedef in C++ Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Leave a Comment