abseil/abseil-cpp

[Bug]: variant index returns wrong value

Opened this issue · 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

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

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.

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.