public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104873] New: Bug in overload resolution for constrained class templates with deduction guides
@ 2022-03-10 21:47 jgopel at gmail dot com
  2022-03-11 16:47 ` [Bug c++/104873] " ppalka at gcc dot gnu.org
  2022-03-11 20:30 ` ppalka at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: jgopel at gmail dot com @ 2022-03-10 21:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104873
           Summary: Bug in overload resolution for constrained class
                    templates with deduction guides
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jgopel at gmail dot com
  Target Milestone: ---

Given the following code:

```cpp
template<typename T>
concept C = true;

template<C T>
struct S {
  S(const S&) { ... }
  S(const T&) { ... }
};

// NOTE: The class is constrained, the deduction guide is not
template<typename T>
S(S<T>) -> S<S<T>>;
```

LLVM selects `S(S)` and GCC selects `S(T)`. I believe LLVM is correct here
because the built in deduction guide for `S(S)` is

```cpp
template<C T>
S(const S<T>&) -> S<T>;
```

which is more constrained. I don't believe there's a case where the code
example as-written is intended. I would suggest emitting a warning and using
the built in deduction guide.

Godbolt: https://godbolt.org/z/T79f164nW
LLVM issue suggesting a warning:
https://github.com/llvm/llvm-project/issues/54310
Relevant wording:
- https://eel.is/c++draft/over.match.class.deduct#1.3
- https://eel.is/c++draft/over.match.best#general-2.10

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

* [Bug c++/104873] Bug in overload resolution for constrained class templates with deduction guides
  2022-03-10 21:47 [Bug c++/104873] New: Bug in overload resolution for constrained class templates with deduction guides jgopel at gmail dot com
@ 2022-03-11 16:47 ` ppalka at gcc dot gnu.org
  2022-03-11 20:30 ` ppalka at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-03-11 16:47 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #1 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Reduced testcase:

template<class T> concept C = true;

template<class T> requires C<T>
struct S {
  S(...);
};

template<class T> S(S<T>) -> S<S<T>>;

using type = decltype(S(S<int>()));
using type = S<int>; // Clang accepts, GCC demands S<S<int>>


The difference is that the copy deduction candidate generated by GCC is
unconstrained:
  template<class T> S(S<T>) -> S<T>;
whereas Clang presumably attaches the constraints of the class template to the
candidate:
  template<class T> requires C<T> S(S<T>) -> S<T>;


This also occurs for guides synthesized from a constructor:

template<class T> concept C = true;

template<class T> requires C<T>
struct S {
  S();
  S(T); // #1
};

template<class T> S(T) -> S<S<T>>; // #2

using type = decltype(S(0)); // Clang selects guide for #1, GCC selects #2
using type = S<int>;         // therefore Clang accepts, GCC demands S<S<int>>


Is it true that a deduction guide inherits the constraints of the class
template?  It's not clear to me according to [over.match.class.deduct].

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

* [Bug c++/104873] Bug in overload resolution for constrained class templates with deduction guides
  2022-03-10 21:47 [Bug c++/104873] New: Bug in overload resolution for constrained class templates with deduction guides jgopel at gmail dot com
  2022-03-11 16:47 ` [Bug c++/104873] " ppalka at gcc dot gnu.org
@ 2022-03-11 20:30 ` ppalka at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-03-11 20:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Here's a testcase that seems to argue for including the class constraints in
the implicit guide's constraints (which already contain the rewritten
constraints of the constructor):

template<class T>
struct A {
  static_assert(!__is_same(T, void));
  static constexpr bool value = true;
};

template<class T> requires (!__is_same(T, void))
struct B {
  B(T*) requires A<T>::value; // #1
  B(T);
};

void* p;
using type = decltype(B(p));
using type = B<void*>;

By not doing so we end up checking the two sets of constraints (the class's and
the constructor's) "out of order", which causes a hard error during constraint
checking of the guide for #1.

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

end of thread, other threads:[~2022-03-11 20:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-10 21:47 [Bug c++/104873] New: Bug in overload resolution for constrained class templates with deduction guides jgopel at gmail dot com
2022-03-11 16:47 ` [Bug c++/104873] " ppalka at gcc dot gnu.org
2022-03-11 20:30 ` ppalka 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).