Drake
Drake C++ Documentation
overloaded.h File Reference

Detailed Description

The "overloaded" variant-visit pattern.

The C++ std::visit (typeswitch dispatch) is very useful for std::variant but doesn't support it natively. There is a commonly used two-line boilerplate that bridges this gap; see https://en.cppreference.com/w/cpp/utility/variant/visit

This file should be included by classes that wish to use the variant visit pattern, i.e.

using MyVariant = std::variant<int, std::string>;
MyVariant v = 5;
std::string result = std::visit<const char*>(overloaded{
[](const int arg) { return "found an int"; },
[](const std::string& arg) { return "found a string"; }
}, v);
EXPECT_EQ(result, "found an int");
Warning
This file must never be included by a header, only by a cc file. This is enforced by a lint rule in tools/lint/drakelint.py.