Table of Contents

Writing articles about modern C++ features is a lot of fun, but what’s even better is to see how you use those new things in real world.

Today I’m happy to present a guest post article from JFT who was so kind to describe his project where he uses several C++17 features.
He shared the process of building a menu that is based on std::any, std::variant and std::optional.

Have a look!

Background  

This article arose from Bartek’s blog regarding std::any where he asked for examples of usage. This followed his excellent series of articles on the new C++17 std::any, std::variant and std::optional features.

As I had already been ‘playing around’ with these when I was learning these new C++17 features (yes, we all have to do the book-work to learn new language features – knowledge suddenly doesn’t get implanted into us, even in Bjarne’s case!), and had produced some code that formed the basis of a command-line menu system as a non-trivial example, I posted a link to this code http://coliru.stacked-crooked.com/a/1a5939ae9ce269d2 as a comment to the blog. Bartek has kindly asked me to produce this guest blog describing this implementation.

Put Simply  

What I developed is a very simple command-line menu class and associated utility functions. These utility functions provide the easy means to obtain console input – which as every C++ programmer knows – is fraught with issues regarding stream state etc etc etc for ‘bad input’.

Then there is the menu class. This enables menus to be created and linked together. A menu item displayed can be either a call to a specified function or to reference another menu – or to return to the previous menu if there was one. So the menus are sort of hierarchical.

Here’s a screenshot that illustrate how it looks like: