std::variant duplicate type cause no suitable constructor error(clang-1300.0.29.3)

  c++, c++17

Just wondering if this is the expected behaviour of std::variant, as well as the reasoning for this behaviour.

simplified code to reproduce the error is as below:

double d= 1.0;
std::variant<std::monostate, double, double> v(d);

The error message is shown as below:

no suitable constructor exists to convert from "double" to "std::__1::variant<std::__1::monostate, std::__1::remove_cv_t<std::__1::remove_reference_t<double &>>, std::__1::remove_cv_t<std::__1::remove_reference_t<double &>>>"C/C++(415)

One solution I found now is to create a metafunction that removes duplicates from the variant type and then construct with the variable of the type double.
i.e.

distinct<std::variant<std::monostate, double, double>>v(d);
//distinct_t<std::variant<std::monostate, double, double>> -> std::variant<std::monostate, double>

If there is any better solution, please also let me know. Thanks in advance.

Source: Windows Questions C++

LEAVE A COMMENT