Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefer std::string in std::variant<bool, std::string> for const char *

Consider the following code -

#include <variant>
#include <string>

int p(std::variant<bool, std::string> v) {
    return v.index();
}

int main() {
    return p("ad");
}

instead of choosing std::string, p will be instantiated with variant containing bool (I want std::string), well this can be fixed using explicitly specifying std::string but that is too much work 😊, I tried providing different overloads but it doesn't seem to work.

like image 378
jaganantharjun Avatar asked Jan 01 '23 05:01

jaganantharjun


1 Answers

This has been fixed in C++20. See P0608 for a discussion of this exact case.

like image 83
Marshall Clow Avatar answered Jan 04 '23 02:01

Marshall Clow