public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "dangelog at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/113060] New: std::variant converting constructor/assignment is non-conforming after P2280? Date: Mon, 18 Dec 2023 09:32:28 +0000 [thread overview] Message-ID: <bug-113060-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113060 Bug ID: 113060 Summary: std::variant converting constructor/assignment is non-conforming after P2280? Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: dangelog at gmail dot com Target Milestone: --- GCC 14 implements P2280 (see #106650). As a side effect of that, the "narrowing detector" used in std::variant's converting constructor/assignment is now too restrictive. This code: // IC is a type with a constexpr conversion operator using IC = std::integral_constant<int, 42>; std::variant<float> v( IC{} ); should work after P2280 (libstdc++ says ill-formed). --- https://eel.is/c++draft/variant.ctor#14 says: > Let Tj be a type that is determined as follows: build an imaginary function FUN(Ti) for each alternative type Ti for which Ti x[] = {std::forward<T>(t)}; is well-formed for some invented variable x. > The overload FUN(Tj) selected by overload resolution for the expression FUN(std::forward<T>(t)) defines the alternative Tj which is the type of the contained value after construction. Right now libstdc++ implements this by means of SFINAE: > https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/variant#L787-L823 IC is convertible to float, and given the constexpr nature of its conversion operator (to int), it wouldn't be a narrowing conversion: IC ic; float f{ic}; // not narrowing However, using SFINAE means that std::declval<IC>() is used to build the "candidate" argument of FUN. declval is not a constexpr function so its returned value is not considered to be usable in constant expressions. The net effect is that `Ti x[] = {std::forward<T>(t)};` is considered to be a narrowing conversion ([dcl.init.list], int->float, source is not a constant expression), and FUN(float) rejected. But nowhere does [variant.ctor] talk about using std::declval; if one reimplements the same check with a constraint, then GCC 14 accepts the conversion: template <typename Tj, typename T> concept FUN_constraint = requires(T &&t) { { std::type_identity_t<Tj[]>{ std::forward<T>(t) } }; }; template <typename T> requires FUN_constraint<float, T> void FUN(float); FUN<IC>( IC{} ); // OK https://gcc.godbolt.org/z/xP9z97v35 P2280 is necessary to make this work, because otherwise the usage of a reference in the requires-expression would make `t` again not usable in constant expressions. In conclusion, it seems that variant<float> should indeed accept construction from IC. (I can't cross-check this report with other compilers as none of them implements P2280 yet.)
next reply other threads:[~2023-12-18 9:32 UTC|newest] Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-12-18 9:32 dangelog at gmail dot com [this message] 2023-12-18 11:18 ` [Bug libstdc++/113060] " redi at gcc dot gnu.org 2023-12-18 14:32 ` dangelog at gmail dot com 2024-02-16 14:43 ` redi at gcc dot gnu.org 2024-02-19 8:07 ` de34 at live dot cn 2024-02-19 9:07 ` de34 at live dot cn 2024-02-19 9:11 ` de34 at live dot cn 2024-02-19 23:47 ` dangelog at gmail dot com 2024-02-20 1:47 ` de34 at live dot cn 2024-02-20 10:09 ` dangelog at gmail dot com
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-113060-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).