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.
-
2Semantics is the branch of linguistics that deals with meaning. So to say that something is "semantically equivalent" generally means that "they mean exactly the same thing, have the same effect on the program." There are some things that that `using` can do that `typedef` cannot (such as be templated) but other than that, both have the same effect on compilation: they both create an alias. – cdhowie May 05 '17 at 16:02
-
2Possible duplicate of [What is the difference between 'typedef' and 'using' in C++11?](http://stackoverflow.com/questions/10747810/what-is-the-difference-between-typedef-and-using-in-c11) – PointerToConstantChar May 05 '17 at 16:17
2 Answers
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.

- 146
- 1
- 10
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
.

- 1,084
- 9
- 28
-
*"And every time I used `set_it
` as a type I got a `std::set – IInspectable May 08 '17 at 23:23::iterator`"* - Uh, yeah. But that is exactly why the `auto` keyword got revised semantics in C++11, so that you do not have to repeat types. That's a really poor sample you picked.