Slide 1

Slide 1 text

JSON, std::variant, and you Chris Jester-Young @cky944 January 2017

Slide 2

Slide 2 text

Code > slides ● Please feel free to check out the code at https://gitlab.com/cky/cpp17-json ● The slides here are just summary notes so you can spend all your time playing with the code instead ☺ ○ You need to apply the fix at http://stackoverflow.com/a/32004040/13 before the tests, or anything using the UTF-16/UTF-32 modes, will compile

Slide 3

Slide 3 text

What is a variant? ● A variant is a type that can hold objects of various types ○ E.g., variant>> ● A variant remembers its current type ○ Retrieve with get(v) ○ Test with get_if(&v)

Slide 4

Slide 4 text

So how is it different from any? ● All possible types known ○ No dynamic allocation ○ Visitation possible

Slide 5

Slide 5 text

Comparison with Boost.Variant ● Multi-level visitation! ● In-place construction, emplace() ● monostate ● No built-in recursive type wrapper ○ Use CRTP instead, or perhaps make your own wrapper

Slide 6

Slide 6 text

Visitation: it’s what’s for dinner ● The clean way to do different things for different types ● Just define operator() that accepts each type ○ Can be member functions as well as template functions, or mix and match ○ Can use enable_if to shape your dispatch however you like ● Variadic visit() means multiple dispatch

Slide 7

Slide 7 text

Current implementations ● As of current writing, most readily available in Visual Studio 2017 RC ● libc++ 4.0 and libstdc++ 7.0 will also have it, but for now you have to build it yourself ● MPark.Variant is written by libc++’s std::variant implementer and provides a backport to C++14

Slide 8

Slide 8 text

Questions? You can find c++17-json at https://gitlab.com/cky/cpp17-json. ● Email me at [email protected] with any questions, or tweet me at @cky944 ● I camp out on Stack Overflow, as well as #stackoverflow on freenode ● https://github.com/cky, https://gitlab.com/cky Thanks for coming to my talk! ❤