abseil-cpp icon indicating copy to clipboard operation
abseil-cpp copied to clipboard

[Bug]: variant index returns wrong value

Open Christoph142 opened this issue 2 years ago • 4 comments

Describe the issue

variant index returns wrong value

Steps to reproduce the problem

std::variant<std::string, bool> test_std = "test"; absl::variant<std::string, bool> test_absl = "test"; std::cout << test_std.index() << " <->" << test_absl.index(); // 0 <-> 1

What version of Abseil are you using?

Current Chromium Code

What operating system and version are you using?

Win 11

What compiler and version are you using?

Current Chromium Code

What build system are you using?

Current Chromium Code

Additional context

No response

Christoph142 avatar Aug 27 '23 12:08 Christoph142

I've managed to reproduce this issue. As a workaround, try: absl::variant<std::string, bool> test_absl = std::string("test");

derekmauro avatar Aug 30 '23 17:08 derekmauro

IIRC, this likely depends on which version of std::variant you've got. There were (numerous) defect reports submitted for variant, which may or may not have been backported to any given implementation.

I believe that absl::variant was matching the semantics of C++17-at-publication, but I've definitely lost track of the details. (There's a deep mess lurking here no matter which way you look at it.)

On Wed, Aug 30, 2023 at 1:53 PM Derek Mauro @.***> wrote:

I've managed to reproduce this issue. As a workaround, try: absl::variant<std::string, bool> test_absl = std::string("test");

— Reply to this email directly, view it on GitHub https://github.com/abseil/abseil-cpp/issues/1525#issuecomment-1699608039, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7CRX2P3FCBLXBS56MH4I3XX543NANCNFSM6AAAAAA4AIR6XA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- *If you get an email from me outside of the 9-5 it is not because I'm always on or expect an immediate response from you; it is because of work flexibility http://www.inc.com/john-boitnott/how-flexible-hours-can-create-a-better-work-life-balance.html . Evening and weekend emails are a sign I allocated some regular working hours for other things (such as family, gym, friends,...). And I encourage you to feel free to do the same.

tituswinters avatar Aug 30 '23 18:08 tituswinters

Thanks for the workaround @derekmauro. Unfortunately it's not that easy. I'm using this in a loop over a (test case) struct. But I managed to avoid it by altering my test case and absl::variant<std::string, int> test_absl = "test"; returns the correct index. Looks like it's only happening with bool in the mix.

Christoph142 avatar Aug 30 '23 18:08 Christoph142

It looks like this is the defect report: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0608r3.html

This case is explicitly mentioned as an example.

derekmauro avatar Aug 30 '23 18:08 derekmauro