You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
determines the alternative to construct as if by selecting an overload for F(std::forward<T>(t)) from an overload set F(T_j) where T_j are types of the alternatives,
e.g. for std::variant<std::monostate, bool, int64_t, double> the overload set would be
toughengineer
changed the title
std::variant converting constructor compiles while the C++ Standard says it must notstd::variant converting constructor and operator= compile while the C++ Standard says they must not
Dec 13, 2023
toughengineer
changed the title
std::variant converting constructor and operator= compile while the C++ Standard says they must not
[libc++] std::variant converting constructor and operator= compile while the C++ Standard says they must not
Dec 13, 2023
According to [\[variant.ctor\]](https://eel.is/c++draft/variant.ctor) (also see [cppreference](https://en.cppreference.com/w/cpp/utility/variant/variant)) converting constructor
```c++
template<class T>
constexpr variant(T&& t) noexcept(/*...*/);
```
determines the alternative to construct as if by selecting an overload for `F(std::forward<T>(t))` from an overload set `F(T_j)` where `T_j` are types of the alternatives,
e.g. for `std::variant<std::monostate, bool, int64_t, double>` the overload set would be
```c++
void F(std::monostate) {}
void F(bool) {}
void F(int64_t) {}
void F(double) {}
```
Libc++ is correct here, see my explanation in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113007
(tl;dr the conversion to bool or double is considered narrowing so those conversions are not allowed, and only int64_t is viable).
According to [variant.ctor] (also see cppreference) converting constructor
determines the alternative to construct as if by selecting an overload for
F(std::forward<T>(t))
from an overload setF(T_j)
whereT_j
are types of the alternatives,e.g. for
std::variant<std::monostate, bool, int64_t, double>
the overload set would beIn violation of the standard this code compiles with Clang and libc++ (https://godbolt.org/z/aah9Pq9fq):
while overload resolution of
F()
fails:compiler error (click/tap to expand)
Also the same applies to converting assignment operator
see [variant.assign] (also cppreference).
Among libc++, libstdc++ and MSVC standard libraries only MSVC standard library gets it according to the standard so you can look at it for reference.
See corresponding bug for libstdc++: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113007
The text was updated successfully, but these errors were encountered: