Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are require-clauses evaluated after parameter substitution inside declarations?

In the C++ standard, N4618/[temp.deduct] (§14.8.2), the following example (§14.8.2/7) demonstrates how template parameter substitution is performed in lexical order:

 template <class T> struct A { using X = typename T::X; };
 template <class T> typename T::X f(typename A<T>::X);
 template <class T> void f(...) { }
 template <class T> auto g(typename A<T>::X) -> typename T::X;
 template <class T> void g(...) { }
 void h() {
    f<int>(0);// OK, substituting return type causes deduction to fail
    g<int>(0);// error, substituting parameter type instantiates A<int>
}

I was expecting requires-clauses content to be also evaluated before declaration content. I expected that the following modification would not generate any compilation error:

 template <class T> struct A { using X = typename T::X; };
 template <class T> typename T::X f(typename A<T>::X);
 template <class T> void f(...) { }
 template <class T>
 requires false
 auto g(typename A<T>::X) -> typename T::X;
 template <class T> 
 requires true
 void g(...) { }
 void h() {
    f<int>(0);// OK, substituting return type causes deduction to fail
    g<int>(0);// error, substituting parameter type instantiates A<int>
}

Actually GCC tell me there is still an error. Is it the behaviour stated in the concept TS?

like image 504
Oliv Avatar asked Mar 10 '26 15:03

Oliv


1 Answers

Yes. See the note in N4630 [temp.deduct]/5:

The satisfaction of constraints (14.10) associated with the function template specialization is determined during overload resolution (13.3), and not at the point of substitution.

like image 76
T.C. Avatar answered Mar 13 '26 05:03

T.C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!