Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is `using A = struct A{};` good for in C++?

I found the construct

using A = struct A{};

in a C++ code base. What is this good for? My guess would be that using A has no effect at all in this context.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As for why it’s valid C++, I would imagine that is due to parity with typedef. Many a C code base have something like this:

typedef struct A {
  // ...
} A;

This is valid C, and for compatibility’s sake, the C++ standard has verbiage to accept this construct (which has no effect). Since a type alias declaration with using is meant to be exactly the same as the equivalent typedef, the validity of what you saw is likely an emergent property.

As for why you saw it, it may just be a C-ism (which often make their way into C++ code) that got translated into "Modern C++". That is of course silly, and a simple structure declaration will do if the code is meant to be C++-only1.



1 – The C standard did add a bunch of features in C23 for increased parity with C++ code. I’m not aware of any plans to allow modern C++ type aliases in C, but assuming there were any, that construct may then be used for compatibility too.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading