Last Update:
Show me your code: std::optional
Table of Contents
Show me your code!
I’d like to run a little experiment.
Let’s build a wall of examples of std::optional
!
Intro
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.
- You can add a link in the comments below or send me an email
- 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.
Dates:
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 };
};
std::optional<SelectionData>
CheckSelection(const ObjSelection &objList)
{
if (!objList.IsValid())
return { };
SelectionData out;
// scan...
return {out};
}
The gift
I have 2 x 25$ Amazon.com 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 Amazon.com 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:
- Refactoring with
std::optional
- Using
std::optional
- Error handling and
std::optional
- Using
std::variant
(this post) - Using
std::any
- In place construction for
std::optional
,std::variant
andstd::any
- Using
std::string_view
- C++17 string searchers & conversion utilities
- Working with
std::filesystem
- Something more? :)
Resources about C++17 STL:
- C++17 In Detail by Bartek!
- C++17 - The Complete Guide by Nicolai Josuttis
- C++ Fundamentals Including C++ 17 by Kate Gregory
- Practical C++14 and C++17 Features - by Giovanni Dicanio
- C++17 STL Cookbook by Jacek Galowicz
I’m waiting for your code!
I've prepared a valuable bonus for you!
Learn all major features of recent C++ Standards on my Reference Cards!
Check it out here: