Partners: KDAB Whole Tomato Software CppDepend

28 May 2018

Show me your code: std::optional

std::optional contest

Show me your code!

I’d like to run a little experiment.

Let’s build a wall of examples of std::optional!


In the last three articles of my C++17 STL series I’ve been discussing how to use std::optional. I can talk and talk… or write and write… but I’m wondering how do you use this wrapper type?

That’s why I prepared a little experiment and a giveaway:

The rules

It’s all about you (short) examples ofstd::optional
Later, I plan to compose a new blog post with all of the submissions.

  • Send me a link to gist/coliru/compiler explorer… etc - with a short example of std::optional.
    • You can add a link in the comments below or send me an email
      • bartlomiej DOT filipek AT bfilipek DOT com
    • Please mention if you allow showing your Name next to the example
    • This submission is one-time only so that I won’t add you to my email list automatically. However, if you’d like to stay updated about the results and future posts, then please subscribe.
  • Ideally the max number of lines is 25 (not taking into account main() or the caller’s code).
    • Feel free to submit the code if it’s longer, we’ll think how to make it more compact.
  • Add description what the code does.
  • The code should represent some “real-life” use.
  • The code cannot, of course, violate any copyright rules.
  • I’ll select most useful examples and compose a single post about optional examples
  • You can submit only one code sample.

Usually std::optional is used in:

  • To return something from a function
  • As an optional input parameter to a function
  • As an optional class member
  • To perform some lazy loading/two-phase init of some object

So probably your code will be one of those three above variations... but of course you might came up with something different.

It starts now! (28th May)
Ends 7th June (8:00 am GMT+2 Time, Poland) (so I can prepare a post that will be published on 11th June)

For a start here’s a Coliru link with some basic sample:
Coliru sample std::optional code

For example this my source code that I’ve shared in some previous posts:

struct SelectionData
    bool anyCivilUnits { false };
    bool anyCombatUnits { false };
    int numAnimating { 0 };

CheckSelection(const ObjSelection &objList)
    if (!objList.IsValid())
        return { };

    SelectionData out;   

    // scan...

    return {out};

The gift

I have 2 x 25$ Gift Card.
I’ll pick two random winners from all the submissions.

Note: It’s an US gift card, so you’ll be able to use it on only.

The Series

This article is part of my series about C++17 Library Utilities. Here’s the list of the other topics that I’ll cover:

Resources about C++17 STL:

I’m waiting for your code!

Get my free ebook about C++17!

More than 50 pages about the new Language Standard.

C++17 in detail, by Bartlomiej Filipek

© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.