0

I'm creating a class representation of a mathematical tuple (or simply, an ordered set of numbers). Being a list-like object that could benefit from an iterator representation, I have decided to give it one. By following this tutorial, I have found little difficulty in creating the iterator class.

What I do not understand, however, is why the author used alias-declarations instead of typedefs when declaring the iterator's tags. I have seen other people doing this as well, such as here. I would prefer to use typedefs as it makes things clearer when creating/reading member functions, but I'm unaware if there are any downsides to doing this.

Edit: Changed using-declarations to alias-declarations, as pointed out by Sebastian Redl

4

In C++, using and typedef are mostly equivalent and both declare a type alias. But they have reverse order of arguments:

typedef original new_name;
using new_name = original;

The typedef can get very difficult to read for more complex types. For example, the meaning of typedef unsigned uint might not be immediately apparent. C's notation for function pointers is famously inscrutable: typedef void (*FuncType)(int a, int b). This syntactic mess also means you can't use a typedef template. Unfortunately, typedef also has very subtle interactions with linkage (which I don't understand and thus can't explain here, sorry).

The type alias with using is generally easier to read and is just a type alias, without other subtle effects. It composes well with templates. It was introduced as part of the big language modernization in C++11. Modern C++ greatly prefers this form, see also T.43 in the C++ Core Guidelines. The using keyword is also used for other constructs that bring something into scope, e.g. namespace aliases or using-declarations for unqualified lookup (argument-dependent lookup).

The syntax used for declaring type aliases does not interact or interfere with member functions.

So you can continue to use typedef without any ill effects. However, the C++ community would thank you if you don't use that C-ism and use the more easier to read using keyword.

3
  • "For simple things" they have reverse order of arguments:
    – Caleth
    May 9 at 21:10
  • I came to C++ from C, so typedef's just come naturally to me. My original plan was to declare all of the types in the beginning of the class and use them as member function parameters.
    – Angel E.
    May 9 at 21:29
  • @AngelE. And it's perfectly fine to do that. C++ is highly compatible with C, so it's OK to just use it as a “better C”.
    – amon
    May 10 at 11:55
2

These aren't using-declarations. They are alias-declarations. Aside from the different syntax, they are 100% semantically identical to typedefs.

I find them a lot more readable, though, and I imagine the author of the tutorial has the same view, which would be reason enough to use them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.