Jonathan Boccara's blog

The C++ Metaclasses Proposal in Less Than 5 Minutes

Published March 9, 2018 - 5 Comments

If you want a glimpse of what the future of C++ might look like, here is a brief overview of one of the most popular recent proposals: metaclasses.

It’s also interesting to know about metaclasses even for the present, because this proposal puts in perspective structuring elements of the language as it is today.

Transcript of the video:

Hello, this is Jonathan Boccara for Fluent C++. Today we’re going to talk about metaclasses.

Metaclasses are a proposal made by Herb Sutter a couple of months ago, and it made a buzz in the C++ community. It’s a proposal worth looking into because it shows the structure of the language, even as of today, and the direction that we want it to take.

So let’s learn more about that.

If you want to define a type you’ve got essentially two options today: struct or class. As we’ve seen in a previous video, how to choose between struct or class is a matter of convention, about what information you want to express about your type.

But the thing is, since it’s just a convention, there’s nothing to enforce it in code. So that’s one limitation of struct and class.

A second limitation is that, in some cases, neither one really fits. Imagine the case of an interface for example, that type with pure virtual methods, a virtual destructor, no private members. What should we use, struct or class? There is nothing private, which suggests that maybe we should use a struct. But on the other hand, it can do things, it embodies responsibilities, so maybe it should rather be a class.

In this case, neither struct nor class fits perfectly. That’s a second limitation of struct and class.

The last one is that it leads to duplicated code. Let’s take the interface again. Actually it’s quite a canonical example with metaclasses. This pattern of having only pure virtual methods, and a virtual destructor, and no data, and no private members, this pattern duplicated over and over in your code every time you create an interface. This leads to the risks of code duplication, for example forgetting the virtual destructor once in a while for example.

Metaclasses expand the way to create a type beyond struct and class. You can define your own metaclasses that is a type of type that allows to define types.

To make it clearer, a class describes what an object looks like, and is used at runtime to instantiate objects. A metaclass describes what a class looks like, and is used at compile time to instantiate classes.

Now the question is: what is inside the code of a metaclass to describe a class?

The metaclass proposal is based on two other proposals: reflection and compile time programming.

Let’s take the example of the interface we were talking about. Reflection allows to access some data about the class, such as: are all the methods public? And to manipulate that data, for example: if a method is private, make it public.

And compile time programming makes checks at compile time, like: is there any private member, or is there any data member, which is not supposed to be the case in an interface. And after performing those compile time checks, compile time programming allows to emit custom compile errors messages such as “an interface cannot have private data”.

Compile time programming also allows to generate code like generate a virtual destructor for example.

So we could define a metaclass interface with all those features, and this interface would be used instead of struct or class when we define an interface, and we wouldn’t have to write the =0 and the virtual destructor because the metaclass would take care of that:

That’s very roughly what’s in the metaclass proposal. If you want to read more details about them you can go check out A summary of the metaclass proposal for C++. And if you have a bit of time ahead of you, here is where you can find the actual proposal.

I hope this video has been useful to you. If you liked it you can subscribe to the channel to get more videos about expressive code in C++, and if you liked it you can share it with your friends and put a thumb up.

Thanks, and I see you next time!

Related posts:

Become a Patron!
Share this post! Facebooktwittergoogle_pluslinkedin    Don't want to miss out ? Follow:   twitterlinkedinrss

Get a free ebook on C++ smart pointers

Get a free ebook of more than 50 pages that will teach you the basic, medium and advanced aspects of C++ smart pointers, by subscribing to our mailing list! On top of that, you will also receive regular updates to make your code more expressive.

No spam. You can unsubscribe at any time.