Exercise 3. std::any from scratch.

In this exercise, you will be implementing std::any by removing functionality from std::function.

Open this wandbox (b, b, b, b, b, b). The header file "function.h" contains a fundamentally complete implementation of std::function (but without the Small Object Optimization). This wandbox also contains a header file named "any.h" that is simply a copy of "function.h".

1. Read "function.h" from top to bottom. Does it make sense? Can you match up the class definitions in the actual code with the pieces of the diagram from today's slide presentation?

2. Modify "any.h" so that it is a valid implementation of std::any. You can try to do it on your own, or you can follow this checklist. The necessary changes are:

Run the tests; do they pass? (They should!)

3. (Optional) Notice that we left any::target() as a public member function, whereas in the actual standard library there is no such function (just any_cast). Make any::target() a private member function and make any_cast a friend of any. In your opinion, which interface seems "better": private-member-function-and-friend, or public-member-function? Why?


You're done with the third set of exercises! Sit back and relax, or optionally, browse the following blog posts.