`overload` helper for variants

46 views
Skip to first unread message

Joe Mason

unread,
Jun 3, 2022, 2:52:12 PM6/3/22
to chromium-dev, cxx
There's a common helper for std::variant (or, for chromium, absl::variant) which is traditionally named "overload": https://www.cppstories.com/2019/02/2lines3featuresoverload.html/.

It allows use of the std::visit function with lambdas.

Without overload:

struct PrintVisitor {
  void operator()(int& i) const { std::cout << "int: " << i; }
  void operator()(float& f) const { std::cout << "float: " << f; }
  void operator()(std::string& s) const { std::cout << "string: " << s; }
};
void Print(std::variant<int, float, std::string> intFloatString) {
  std::visit(PrintVisitor(), intFloatString);
}

With overload:

void Print(std::variant<int, float, std::string> intFloatString) {
  std::visit(overload  {
      [](const int& i) { std::cout << "int: " << i; },
      [](const float& f) { std::cout << "float: " << f; },
      [](const std::string& s) { std::cout << "string: " << s; },
    },
    intFloatString);
}

The implementation uses some C++17 features. Now that C++17 is supported, should we land this helper as `base::overload`?

I'm not fond of the name - I'd prefer something more explicit like "lambda_visitor" - but I don't think it's worth diverging from the common practice. Using a different name would be less discoverable.

Joe

Peter Kasting

unread,
Jun 3, 2022, 2:56:20 PM6/3/22
to Joe Mason, chromium-dev, cxx
Is this scheduled for inclusion in a future version of the STL? If yes, let's pick that name.

PK

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAH%3DT95TCF_GyXWbVRKBvErm645Tk3ju0_u_X_10av6gXdvD3OQ%40mail.gmail.com.

Joe Mason

unread,
Jun 3, 2022, 3:07:39 PM6/3/22
to Peter Kasting, chromium-dev, cxx
I haven't heard anything about it being slated for inclusion in the STL. I don't know why not - it seems too useful to leave out. But everywhere I see it referenced defines it in-place (eg. in the example code for std::visit at cppreference.comhttps://en.cppreference.com/w/cpp/utility/variant/visit).

And I now note that I've sometimes seen it called `overload`, and sometimes `overloaded`. Searching for "std::overloaded" gets a lot more hits, so maybe I missed a lot of discussion.

Joe Mason

unread,
Jun 3, 2022, 3:10:59 PM6/3/22
to Peter Kasting, chromium-dev, cxx

Dmitrii Kuragin

unread,
Jun 4, 2022, 4:41:13 PM6/4/22
to Chromium-dev, Joe Mason, chromium-dev, cxx, Peter Kasting
It might have been implemented in base, but I assume one of the reason it is not implemented there is it is simple to implement on your own.


It is TBD.

Joe Mason

unread,
Jun 6, 2022, 4:10:35 PM6/6/22
to Dmitrii Kuragin, Chromium-dev, cxx, Peter Kasting
It may be simple to copy those two lines, but it's deep compiler magic that really should be wrapped up in a library function, to prevent boilerplate code that few people understand from proliferating. There's also benefit to standardizing on one name instead of having `foo::overload` and `bar::overloaded` in many places.

Part 1 of landing a patch would be to get approval to use fold expressions (either generally, or just to implement this). I believe it's possible to implement this without them, it's just much more verbose. I thought I'd get consensus on whether adding the function was a good idea before opening that topic.

For the record, I haven't landed anything that would use `overloaded`, but I've had 2 WIP CL's that could have used it except that I refactored them to not use variants for other reasons before submitting them. Not sure if that's a signal that the function is useful (because I almost used it twice), or that it's not useful (because both times I found a better way).

Daniel Cheng

unread,
Jun 6, 2022, 4:17:27 PM6/6/22
to Joe Mason, Dmitrii Kuragin, Chromium-dev, cxx, Peter Kasting
I think it's fine to add it. Maybe add it in something like variant_utils.h?

It'd be nice to simplify things like https://source.chromium.org/chromium/chromium/src/+/main:base/values.cc;l=291;drc=6b621cf5ec249e2a0b1fe8d9a58427f813fdb339. "if constexpr" works but isn't the nicest to read.

Daniel

Peter Kasting

unread,
Jun 6, 2022, 4:17:32 PM6/6/22
to Joe Mason, Dmitrii Kuragin, Chromium-dev, cxx
On Mon, Jun 6, 2022 at 1:10 PM Joe Mason <joenot...@google.com> wrote:
Part 1 of landing a patch would be to get approval to use fold expressions (either generally, or just to implement this).

You're welcome to spin that discussion up, FWIW, as even without landing this yet I think the fact that it _could_ use them is reason enough to discuss whether to allow them.  That way you won't be blocked if we decide to move forward here.

PK 

Joe Mason

unread,
Aug 23, 2022, 12:03:15 PM8/23/22
to Peter Kasting, Dmitrii Kuragin, Chromium-dev, cxx
This is now available as base::Overloaded, in base/functional/overloaded.h.

Re-reading this thread, I realized that I forgot to get approval for fold expressions first, so I'll do that post-facto...

Daniel Cheng

unread,
Aug 23, 2022, 2:21:01 PM8/23/22
to Joe Mason, Peter Kasting, Dmitrii Kuragin, Chromium-dev, cxx

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
Reply all
Reply to author
Forward
0 new messages