By Klaim


2012-05-25 02:39:51 8 Comments

I know that in C++11 we can now use using to write type alias, like typedefs:

typedef int MyInt;

Is, from what I understand, equivalent to:

using MyInt = int;

And that new syntax emerged from the effort to have a way to express "template typedef":

template< class T > using MyType = AnotherType< T, MyAllocatorType >;

But, with the first two non-template examples, are there any other subtle differences in the standard? For example, typedefs do aliasing in a "weak" way. That is it does not create a new type but only a new name (conversions are implicit between those names).

Is it the same with using or does it generate a new type? Are there any differences?

6 comments

@marski 2019-12-02 22:23:42

Both keywords are equivalent, but there are a few caveats. One is that declaring a function pointer with using T = int (*)(int, int); is clearer than with typedef int (*T)(int, int);. Second is that template alias form is not possible with typedef. Third is that exposing C API would require typedef in public headers.

@RoboticForest 2019-06-12 02:59:59

I know the original poster has a great answer, but for anyone stumbling on this thread like I have there's an important note from the proposal that I think adds something of value to the discussion here, particularly to concerns in the comments about if the typedef keyword is going to be marked as deprecated in the future, or removed for being redundant/old:

It has been suggested to (re)use the keyword typedef ... to introduce template aliases:

template<class T>
  typedef std::vector<T, MyAllocator<T> > Vec;

That notation has the advantage of using a keyword already known to introduce a type alias. However, it also displays several disavantages [sic] among which the confusion of using a keyword known to introduce an alias for a type-name in a context where the alias does not designate a type, but a template; Vec is not an alias for a type, and should not be taken for a typedef-name. The name Vec is a name for the family std::vector<•, MyAllocator<•> > – where the bullet is a placeholder for a type-name.Consequently we do not propose the “typedef” syntax.On the other hand the sentence

template<class T>
  using Vec = std::vector<T, MyAllocator<T> >;

can be read/interpreted as: from now on, I’ll be using Vec<T> as a synonym for std::vector<T, MyAllocator<T> >. With that reading, the new syntax for aliasing seems reasonably logical.

To me, this implies continued support for the typedef keyword in C++ because it can still make code more readable and understandable.

Updating the using keyword was specifically for templates, and (as was pointed out in the accepted answer) when you are working with non-templates using and typedef are mechanically identical, so the choice is totally up to the programmer on the grounds of readability and communication of intent.

@Zhongming Qu 2015-10-05 22:58:29

They are largely the same, except that:

The alias declaration is compatible with templates, whereas the C style typedef is not.

@g24l 2015-11-12 13:34:56

Particularly fond of the simplicity of the answer and pointing out the origin of typeset.

@Marc.2377 2019-09-14 05:23:27

@g24l you mean typedef... probably

@Validus Oculus 2018-03-31 08:20:30

They are essentially the same but using provides alias templates which is quite useful. One good example I could find is as follows:

namespace std {
 template<typename T> using add_const_t = typename add_const<T>::type;
}

So, we can use std::add_const_t<T> instead of typename std::add_const<T>::type

@someonewithpc 2019-03-10 16:15:11

As far as I know, it's undefined behavior to add anything inside the std namespace

@Validus Oculus 2019-03-11 02:18:40

@someonewithpc I was not adding anything, it already exists, I was just showing an example of typename usage. Please check en.cppreference.com/w/cpp/types/add_cv

@4xy 2014-04-21 11:39:59

The using syntax has an advantage when used within templates. If you need the type abstraction, but also need to keep template parameter to be possible to be specified in future. You should write something like this.

template <typename T> struct whatever {};

template <typename T> struct rebind
{
  typedef whatever<T> type; // to make it possible to substitue the whatever in future.
};

rebind<int>::type variable;

template <typename U> struct bar { typename rebind<U>::type _var_member; }

But using syntax simplifies this use case.

template <typename T> using my_type = whatever<T>;

my_type<int> variable;
template <typename U> struct baz { my_type<U> _var_member; }

@Klaim 2014-05-02 19:57:40

I already pointed this in the question though. My question is about if you don't use template is there any difference with typedef. As, for example, when you use 'Foo foo{init_value};' instead of 'Foo foo(init_value)' both are supposed to do the same thing but don't foillow exactly the same rules. So I was wondering if there was a similar hidden difference with using/typedef.

@Jesse Good 2012-05-25 03:16:42

They are equivalent, from the standard (emphasis mine) (7.1.3.2):

A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier. In particular, it does not define a new type and it shall not appear in the type-id.

@iammilind 2012-05-25 04:28:14

From the answer, using keyword seems to be a superset of typedef. Then, will typdef be deprecated in future ?

@bames53 2012-05-25 05:09:59

@iammilind probably not, but you can code as if it were.

@Klaim 2012-05-25 05:11:26

@iammilind I don't think so as it is part of C and certainly will not be removed from it. Also, C++11 and future versions will still keep maximum compatibility with our current code...

@Iron Savior 2013-04-20 20:37:40

Deprecation doesn't necessarily indicate intent to remove--It merely stands as a very strong recommendation to prefer another means.

@TemplateRex 2013-05-09 21:59:01

@iammilind typedef is unlikely to become extinct. For one, the current Standard Library design guidelines have a clause "where C++ 2003 syntax can be used, they are preferred over C++ 2011 features". Of course, in user code no such restrictions apply.

@celtschk 2013-07-21 09:44:23

But then I wonder why they didn't just allow typedef to be templated. I remember having read somewhere that they introduced the using syntax precisely because the typedef semantics didn't work well with templates. Which somehow contradicts the fact that using is defined to have exactly the same semantics.

@Jesse Good 2013-07-21 22:19:07

@celtschk: The reason is talked about in the proposal n1489. A template alias is not an alias for a type, but an alias for a group of templates. To make a distinction between typedef the felt a need for new syntax. Also, keep in mind the OP's question is about the difference between non-template versions.

@davidA 2014-05-08 02:13:55

What about with template-templates? A typedef for one of the template template parameters doesn't appear to be suitable as the typedef isn't correct unless it itself has template parameters specified. Can the type alias be used here, or is it equivalent to a typedef in this case (and can't be used)? Or is the solution to this a template type alias? That's a difference between a type alias and a typedef then, yes?

@Benoît 2017-04-29 20:31:53

So why this redundancy whas introduced ? 2 syntaxes for the same purpose. And I don't see typdef being deprecated ever.

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] What is the "-->" operator in C++?

9 Answered Questions

[SOLVED] What is a lambda expression in C++11?

37 Answered Questions

13 Answered Questions

[SOLVED] What is a smart pointer and when should I use one?

11 Answered Questions

[SOLVED] What is move semantics?

27 Answered Questions

13 Answered Questions

[SOLVED] What is a typedef enum in Objective-C?

8 Answered Questions

[SOLVED] Difference between 'struct' and 'typedef struct' in C++?

  • 2009-03-04 20:41:12
  • criddell
  • 496445 View
  • 803 Score
  • 8 Answer
  • Tags:   c++ struct typedef

11 Answered Questions

[SOLVED] What does the explicit keyword mean?

Sponsored Content