Using "typedef" or "using" to define a structure – which is best?


Sample structure:

typedef struct tagExportSettings
    COLORREF    crHeading{};
    COLORREF    crEvenBack{};
    COLORREF    crOddBack{};
    COLORREF    crHighlight{};
    COLORREF    crDate{};
    COLORREF    crEvent{};
    COLORREF    crEmpty{};
    COLORREF    crNotes{};


Visual Assist says:

typedef can be converted to using declaration.

Is there any real benefit of making this code change?

All my code in the software uses EXPORT_SETTINGS_S so I don’t want to break that syntax.


Even better is to use neither. One type name should be enough. Pick either tagExportSettings or EXPORT_SETTINGS_S and stick with it. Example:

struct tagExportSettings
    // ...

But, 1. All my code in the software uses EXPORT_SETTINGS_S

As I said, pick either name. If you use EXPORT_SETTINGS_S, then name the class as EXPORT_SETTINGS_S:

    // ...

If something still refers to tagExportSettings, then refactor the code to use the canonical name.

But more generally, using is preferred to typedef because it’s more readable. There are at least two reasons for it:

  1. With typedef syntax it isn’t intuitive which is the old name and which is the new alias:

    typedef new_or_old old_or_new; // old_or_new is the new alias

    using is intuitive through familiar pattern of initialisation:

    using intuitively_alias = intuitively_old_name;
  2. typedef syntax is difficult for a programmer to parse in case of compound names because the alias is "interleaved":

    // alias for void()
    typedef void function();
    using function = void();

Answered By – eerorika

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.