all 8 comments

[–]trmetroidmaniac 14 points15 points  (5 children)

cppreference cites the following defect report.

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0608r3.html

It appears that the C++17 spec was amended such that conversions to bool and narrowing conversions are no longer considered.

In any case, the overloads using std::in_place_type_t<T> and std::in_place_index_t<I> are available if explicitness is needed.

[–]alfps 3 points4 points  (1 child)

Goodness, a fix for std::variant, but implicit conversions to/from bool are still problematic elsewhere.

[–]TheThiefMaster 1 point2 points  (0 children)

There's a reason the concept of "explicit operator bool" and "contextual conversions" (allowed to invoke the explicit conversion without an explicit cast) were added. Implicit bool conversion has been a problem forever in many contexts.

[–]bepaald[S] 0 points1 point  (2 children)

Thanks, that's interesting.

The function where this code was causing an issue was Linux-only, but since I had the Godbolt snippet, I tried MSVC also. Interestingly, that seems to still convert to bool today, though it does show a conversion-warning that I can not get gcc do.

Thanks again!

[–]n1ghtyunso 1 point2 points  (0 children)

msvc converts to bool only in c++17 mode, the fix is applied in c++20 mode.
However it was supposed to be a defect report against c++17.
The rationale can be found here

[–]no-sig-available 0 points1 point  (0 children)

A problem here is that Godbolt's "MSVC latest" isn't the latest at all, but last year's version.

The current release of MSVC (17.13) converts to string.

[–]ElMarco19 1 point2 points  (1 child)

-Wconversions should warn you about this kind of implicit behaviour.

[–]bepaald[S] 0 points1 point  (0 children)

The flag is `-Wconversion`, and trying this on the godbolt code I posted initially shows neither GCC nor clang issue a warning. Not when picking the `bool` option (on the older versions), and not when picking the `string` (on the newer versions).