[Bug]: variant index returns wrong value
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");
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.
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.