Consider the following code:
void fnc(int)
{
std::cout << "int";
}
void fnc(long double)
{
std::cout << "long double";
}
int main()
{
fnc(42.3); // error
}
It gives an error because of an ambiguous call to fnc.
However, if we write the next code:
std::variant<int, long double> v{42.3};
std::cout << v.index();
the output is 1, which demonstrates that the double->long double conversion has been chosen.
As far as I know, std::variant follows the C++ rules about conversion ranks, but this example shows the difference. Is there any explanation for such a behavior?
T_i x[] = { std::forward<T>(t) };(en.cppreference.com/w/cpp/utility/variant/variant). This cannot be used forintsince narrowing conversion is not allowed with list-initialization. So, theintvariant is not considered for overloading. Here is a link to the Standard: eel.is/c++draft/variant.ctor#14.sentence-1.long double:std::variant<int, long double> v{ 42.3L };x.