cplusplus / draft

C++ standards drafts
http://www.open-std.org/jtc1/sc22/wg21/
5.73k stars 751 forks source link

[temp.deduct] The substitution in deduction process that results failure should also apply to class templates CWG2054 #5453

Open xmh0511 opened 2 years ago

xmh0511 commented 2 years ago

In the current draft, all interpretations of the deduction failures that occur in the deduction of template arguments for class templates want to refer to [temp.deduct] subclause. However, amount of rules defined in [temp.deduct] hints that they are just defined for function templates. partially cite them out here

The resulting substituted and adjusted function type is used as the type of the function template for template argument deduction. ... If the substitution results in an invalid type, as described above, type deduction fails.

If type deduction has not yet failed, then all uses of template parameters in the function type are replaced with the corresponding deduced or default argument values. If the substitution results in an invalid type, as described above, type deduction fails.

Only invalid types and expressions in the immediate context of the function type, its template parameter types, and its explicit-specifier can result in a deduction failure.

The deduction failure can occur in the deduction of template arguments of a class template, especially the deduction for partial specialization. Consider this case

template<class T, class U = void>
struct A{};

/* int fun(...); */
template<class T>
struct A<T, decltype((void)fun(T{}))>{};

int main(){
   A<int> a;
}

[temp.spec.partial.match] p2

A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list, and the deduced template arguments satisfy the associated constraints of the partial specialization, if any.

If it had not an appropriate fun, the substitution would result in an invalid expression, which causes the deduction failure. The cross-reference of deduce wants to borrow the rules defined for function templates. However, as pointed out above. The deduction failures only apply to function templates. A suggestion is that we should explicitly say a rewritten function template will be generated associated with the class template in order to make all rules for deductions apply to that context.

frederick-vs-ja commented 2 years ago

This is CWG2054.