Reputation:
What does it mean the "typedef" and "using" are semantically equivalent? I am mostly focusing on the words "semantically equivalent". I have tried to look "semantically" up, but it did not really mention any synonym or meaning that I could associate with the usage of the word in the programming world.
Upvotes: 2
Views: 123
Reputation: 1124
You may think about using
as an alias:
using a = b<c>;
can be treated like "As a type, a
means b<c>
".
typedef
does absolutely the same thing, but it comes from C language and uses the same syntax as variable declarations, and it is hard to read them when used with multiple-layered templates, arrays, cv-classifiers, function types etc:
typedef int const* (*func_t)(std::vector<int> const&);
// The same:
using func_t = int const* (*)(std::vector<int> const&);
For me, using
is a lot better because you can easily see the name.
Another difference is that you can use using
with template arguments. For some time, I used this in my hacking code:
template<typename T>
using set_it = std::set<T>::iterator;
And every time I used set_it<int>
as a type I got a std::set<int>::iterator
. You cannot do this with typedef
.
Upvotes: 1
Reputation: 146
It just means they do the same thing, Even if they are slightly different in syntax*. For example the following:
typedef int INTEGER;
Can be written with the using
syntax as follows:
using INTEGER = int;
*The using syntax works also with templates where typedef doesn't, But for non-templates they are equivalent.
Upvotes: 1