public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
@ 2020-06-23  4:48 fiesh at zefix dot tv
  2020-06-23  5:04 ` [Bug libstdc++/95832] " fiesh at zefix dot tv
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2020-06-23  4:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

            Bug ID: 95832
           Summary: std::vector<bool> specialization leading std::variant
                    ctor treating it as int rather than bool
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fiesh at zefix dot tv
  Target Milestone: ---

The following code returned 1 when compiled with gcc-9 but returns 0 with
gcc-10.  Since clang exhibits the exact same behavior depending on the
installed gcc version, I suppose it might be because of libstdc++.

It's worth noting that MSVC on godbolt also returns 0.

My primitive reading skills suggest to me that
http://eel.is/c++draft/variant#ctor-14 says it should be 1.  (Overload
resolution works correctly in that it prefers an f(bool) over an f(int) when
passed b.back().)

#include <vector>
#include <variant>

using T = std::variant<int, bool>;

int main()
{
    std::vector<bool> b{false};
    std::vector<T> ts;
    ts.emplace_back(b.back());
    return ts.back().index();
}

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/95832] std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
  2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
@ 2020-06-23  5:04 ` fiesh at zefix dot tv
  2020-06-23  6:48 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2020-06-23  5:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

--- Comment #1 from fiesh at zefix dot tv ---
https://en.cppreference.com/w/cpp/utility/variant/variant says that

"If T_i is (possibly cv-qualified) bool, F(T_i) is only considered if
std::decay_t<T> (until C++20)std::remove_cvref_t<T> (since C++20) is also bool"

which would mean 0 is the correct return value.  But I can't find anything like
this in

http://eel.is/c++draft/variant

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/95832] std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
  2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
  2020-06-23  5:04 ` [Bug libstdc++/95832] " fiesh at zefix dot tv
@ 2020-06-23  6:48 ` redi at gcc dot gnu.org
  2020-06-23  7:22 ` fiesh at zefix dot tv
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-06-23  6:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
cppreference and libstdc++ both need to be updated to implement
http://wg21.link/p1957

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/95832] std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
  2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
  2020-06-23  5:04 ` [Bug libstdc++/95832] " fiesh at zefix dot tv
  2020-06-23  6:48 ` redi at gcc dot gnu.org
@ 2020-06-23  7:22 ` fiesh at zefix dot tv
  2020-06-23  7:39 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2020-06-23  7:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

--- Comment #3 from fiesh at zefix dot tv ---
I did the former, you do the latter :P

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/95832] std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
  2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
                   ` (2 preceding siblings ...)
  2020-06-23  7:22 ` fiesh at zefix dot tv
@ 2020-06-23  7:39 ` redi at gcc dot gnu.org
  2020-06-23  9:27 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-06-23  7:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-06-23
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Target Milestone|---                         |11.0

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to fiesh from comment #0)
> The following code returned 1 when compiled with gcc-9 but returns 0 with
> gcc-10.

That's because gcc-10 implements http://wg21.link/p0608r3

GCC 10 doesn't implement http://wg21.link/p1957r2 so we still need the
additional constraint in variant's constructors that P0608R3 added: "and if Ti
is cv bool, remove_cvref_t<T> is bool."

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/95832] std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
  2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
                   ` (3 preceding siblings ...)
  2020-06-23  7:39 ` redi at gcc dot gnu.org
@ 2020-06-23  9:27 ` cvs-commit at gcc dot gnu.org
  2020-06-23  9:29 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-06-23  9:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:c98fc4eb3afeda6ad8220d0d79bc1247a92c7c65

commit r11-1598-gc98fc4eb3afeda6ad8220d0d79bc1247a92c7c65
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 23 10:24:49 2020 +0100

    libstdc++: Implement P1972R2 changes to std::variant (PR 95832)

    G++ implements P1972R2 since r11-1597-0ca22d027ecc and so we no longer
    need the P0608R3 special case to prevent narrowing conversions to bool.

    Since non-GNU compilers don't necessarily implment P1972R2 yet, this
    may cause a regression for those compilers. There is no feature-test
    macro we can use to detect it though, so we'll have to live with it.

    libstdc++-v3/ChangeLog:

            PR libstdc++/95832
            * include/std/variant (__detail::__variant::_Build_FUN): Remove
            partial specialization to prevent narrowing conversions to bool.
            * testsuite/20_util/variant/compile.cc: Test non-narrowing
            conversions to bool.
            * testsuite/20_util/variant/run.cc: Likewise.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/95832] std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
  2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
                   ` (4 preceding siblings ...)
  2020-06-23  9:27 ` cvs-commit at gcc dot gnu.org
@ 2020-06-23  9:29 ` redi at gcc dot gnu.org
  2020-06-23 14:44 ` fiesh at zefix dot tv
  2020-06-23 15:36 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-06-23  9:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed on master.

Not possible to backport to gcc-10, because the front end support
isn't there. That unfortunately means std::variant construction works
differently in each of gcc-9, gcc-10 and master.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/95832] std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
  2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
                   ` (5 preceding siblings ...)
  2020-06-23  9:29 ` redi at gcc dot gnu.org
@ 2020-06-23 14:44 ` fiesh at zefix dot tv
  2020-06-23 15:36 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: fiesh at zefix dot tv @ 2020-06-23 14:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

--- Comment #7 from fiesh at zefix dot tv ---
> Not possible to backport to gcc-10, because the front end support
isn't there. That unfortunately means std::variant construction works
differently in each of gcc-9, gcc-10 and master.

Doesn't the new formulation basically revert the change for bool but not pick
bool when given a pointer?  If so, couldn't this be implemented without the
front end considering "pointer to bool" narrowing by checking if the argument
type is a pointer and, if so, not construct the bool?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug libstdc++/95832] std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool
  2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
                   ` (6 preceding siblings ...)
  2020-06-23 14:44 ` fiesh at zefix dot tv
@ 2020-06-23 15:36 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2020-06-23 15:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95832

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
No, that doesn't work for things convertible to pointers.

#include <variant>
struct P { operator void*() const; };
std::variant<bool> v{ P{} };

The argument type here is not a pointer, so your suggestion would construct the
bool, which is exactly the case P0602 was designed to prevent.

"Can be converted to bool via conversion to a pointer or function pointer type"
is not practical to detect, and I have no intention of trying. See Bug 93628
for a related problem.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-06-23 15:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-23  4:48 [Bug libstdc++/95832] New: std::vector<bool> specialization leading std::variant ctor treating it as int rather than bool fiesh at zefix dot tv
2020-06-23  5:04 ` [Bug libstdc++/95832] " fiesh at zefix dot tv
2020-06-23  6:48 ` redi at gcc dot gnu.org
2020-06-23  7:22 ` fiesh at zefix dot tv
2020-06-23  7:39 ` redi at gcc dot gnu.org
2020-06-23  9:27 ` cvs-commit at gcc dot gnu.org
2020-06-23  9:29 ` redi at gcc dot gnu.org
2020-06-23 14:44 ` fiesh at zefix dot tv
2020-06-23 15:36 ` redi at gcc dot gnu.org

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).