public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108536] New: Difference when using requires and enable_if with class constructor
@ 2023-01-25  9:44 hr.jonas.hansen at gmail dot com
  2023-01-25 10:40 ` [Bug c++/108536] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: hr.jonas.hansen at gmail dot com @ 2023-01-25  9:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108536
           Summary: Difference when using requires and enable_if with
                    class constructor
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hr.jonas.hansen at gmail dot com
  Target Milestone: ---

In the code below I have used a requires-clause. This requires-clause used to
be an enable_if. When using enable_if the code compiles without errors, but
using the requires-clause (see below) causes a compilation error when combined
with the rest of the example. That is, the example contains two classes ClassA
and ClassB. If either of the classes ClassA and ClassB are removed then the
code compiles without errors.


Compile with: g++ -std=c++20 example.cpp



#include <type_traits>

struct Base {
    Base() noexcept = default;

    template <typename F, typename DecayF = std::decay_t<F>>
    // If this requires-clause is replaces with an enable_if then the code
compiles fine
        requires(!std::is_same_v<DecayF, Base>
                 && std::is_constructible_v<DecayF, F>)
    Base(F&&) {}
};

struct Derived : public Base {
    using Base::Base;
    void operator()() const;
};

class ClassA {
    // The class ClassB must be present for the bug to manifest
    class ClassB;

    // This is the only usage of 'Derived'
    Derived const f;
};

// This class and its contructor must be included for the bug to manifest
class ClassA::ClassB {
    ClassB();
};

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

* [Bug c++/108536] Difference when using requires and enable_if with class constructor
  2023-01-25  9:44 [Bug c++/108536] New: Difference when using requires and enable_if with class constructor hr.jonas.hansen at gmail dot com
@ 2023-01-25 10:40 ` redi at gcc dot gnu.org
  2023-01-25 11:21 ` hr.jonas.hansen at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2023-01-25 10:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is not a bug, you've just transformed your working code to use a
requires-clause incorrectly.

This works fine:

    template <typename F, typename DecayF = std::decay_t<F>>
        requires (!std::derived_from<DecayF, Base>)
                 && std::constructible_from<DecayF, F>
    Base(F&&) {}

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

* [Bug c++/108536] Difference when using requires and enable_if with class constructor
  2023-01-25  9:44 [Bug c++/108536] New: Difference when using requires and enable_if with class constructor hr.jonas.hansen at gmail dot com
  2023-01-25 10:40 ` [Bug c++/108536] " redi at gcc dot gnu.org
@ 2023-01-25 11:21 ` hr.jonas.hansen at gmail dot com
  2023-01-25 11:45 ` redi at gcc dot gnu.org
  2023-01-25 16:42 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: hr.jonas.hansen at gmail dot com @ 2023-01-25 11:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from hr.jonas.hansen at gmail dot com ---
I can see that, and that would work. But it really seems like a work-around? Is
there a reason for the difference in behavior?

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

* [Bug c++/108536] Difference when using requires and enable_if with class constructor
  2023-01-25  9:44 [Bug c++/108536] New: Difference when using requires and enable_if with class constructor hr.jonas.hansen at gmail dot com
  2023-01-25 10:40 ` [Bug c++/108536] " redi at gcc dot gnu.org
  2023-01-25 11:21 ` hr.jonas.hansen at gmail dot com
@ 2023-01-25 11:45 ` redi at gcc dot gnu.org
  2023-01-25 16:42 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2023-01-25 11:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Because a requires-clause is not just different syntax for enable_if, it works
differently. Different things are different. 

If you want exactly the same behaviour as your enable_if version (which you
didn't show here) then just use that.

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

* [Bug c++/108536] Difference when using requires and enable_if with class constructor
  2023-01-25  9:44 [Bug c++/108536] New: Difference when using requires and enable_if with class constructor hr.jonas.hansen at gmail dot com
                   ` (2 preceding siblings ...)
  2023-01-25 11:45 ` redi at gcc dot gnu.org
@ 2023-01-25 16:42 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-01-25 16:42 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
invalid as explained .

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

end of thread, other threads:[~2023-01-25 16:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-25  9:44 [Bug c++/108536] New: Difference when using requires and enable_if with class constructor hr.jonas.hansen at gmail dot com
2023-01-25 10:40 ` [Bug c++/108536] " redi at gcc dot gnu.org
2023-01-25 11:21 ` hr.jonas.hansen at gmail dot com
2023-01-25 11:45 ` redi at gcc dot gnu.org
2023-01-25 16:42 ` pinskia 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).