Issue 233534
Summary [clang] Explicit template argument: class type of a by-reference parameter is instantiated before the requires-clause is checked
Labels clang
Assignees
Reporter Fedr
    https://godbolt.org/z/6WrdeezPq

```c++
#include <type_traits>

template <class T> struct Fn { static_assert(std::is_function_v<T>); };
template <class T> struct Vec {};
struct C {};

template <class T> void f(const Vec<T>&);
template <class T> requires std::is_function_v<T> void f(const Fn<T>&);

void test(Vec<C> v) {
    f<C>(v);
}
```

Clang 10 through trunk reject this with `static assertion failed` at `Fn<C>`, "in instantiation of template class 'Fn<C>' requested here ... while substituting deduced template arguments into function template 'f' [with T = C]". GCC (13 to trunk) and MSVC (19.44, 19.51) accept it: the constrained overload is dropped because `std::is_function_v<C>` is false.

Expected: the constraint of the second overload is checked and not satisfied, so it is not a candidate and `Fn<C>` is never instantiated. The explicit argument is substituted into `const Fn<C>&`, which needs no complete type ([temp.deduct.general]/2), the constraints are checked once the template arguments are known ([temp.deduct.general]/5), and [temp.deduct.call]/4 only needs a complete `Fn<C>` when the argument type is checked against a possible base class of `P`. The parameter `Vec<C>` is not a class derived from `Fn<C>`, and for a `Vec<C>` argument that check should not be reached for a candidate with unsatisfied constraints.

Contrasts, all accepted by Clang:
- `const Fn<T>*` instead of `const Fn<T>&` (with a `const Vec<T>*` overload and `&v`);
- the call without the explicit argument, `f(v)`;
- C++17 `template <class T, std::enable_if_t<std::is_function_v<T>, int> = 0> void f(const Fn<T>&);` with `f<C>(v)`.

So only a requires-clause with an explicit template argument makes Clang complete the class type. CWG2369 (Clang 20, #102857 and #122423) changed nothing here: the result is identical on 19, 20, 21, 22 and trunk. Related but different: #25268 (CWG1391, conversion check of non-dependent parameters after explicit arguments).

This matters in practice with MS STL, where the primary `std::function` template is defined with a `static_assert`, so `template <class T> requires std::is_function_v<T> size_t heapBytes(const std::function<T>&)` called as `heapBytes<MyStruct>(vec)` is a hard error with clang-cl (libstdc++ and libc++ leave the primary undefined, so Linux never shows it).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to