cplusplus / CWG

Core Working Group
23 stars 7 forks source link

[temp.over] shoudl apply to all context when overload resolution is done for function templates #426

Closed xmh0511 closed 10 months ago

xmh0511 commented 10 months ago

Full name of submitter (unless configured in github; will be published with the issue): Jim X

Consider this case:

template<class T>
void fun(){}

void fun(){}

int main(){
    fun;
}

In this case, only MSVC accepts the code. https://godbolt.org/z/KEvKTEaGq.

[over.over] p2 says

If there is no target, all non-template functions named are selected.

[over.over] p3 says:

The specialization, if any, generated by template argument deduction ([temp.over], [temp.deduct.funcaddr], [temp.arg.explicit]) for each function template named is added to the set of selected functions considered.

hence, [temp.over] and [temp.deduct.funcaddr] determine whether the example is ok. [temp.deduct.funcaddr] says

Template arguments can be deduced from the type specified when taking the address of an overload set. If there is a target ... Otherwise, deduction is performed with empty sets of types P and A.

[temp.deduct.type] p2 says

If type deduction cannot be done for any P/A pair, or if for any pair the deduction leads to more than one possible set of deduced values, or if different pairs yield different deduced values, or if any template argument remains neither deduced nor explicitly specified, template argument deduction fails.

However, except that [temp.over] says that the function tempaltes whose deduction fails are not added to the candidate set, there is no other place say so.

BUT, [temp.over] says

When a call of a function or function template is written (explicitly, or implicitly using the operator notation), template argument deduction ([temp.deduct]) and checking of any explicit template arguments ([temp.arg]) are performed for each function template to find the template argument values (if any) that can be used with that function template to instantiate a function template specialization that can be invoked with the call arguments or, for conversion function templates, that can convert to the required type. For each function template:

  • [...]
  • If the argument deduction fails or the synthesized function template specialization would be ill-formed, no such function is added to the set of candidate functions for that template.

Note the emphasized part, this subclase seems to only applied to the function template call. In this example, the context is the take address of the function template.

Suggested Resolution

[temp.over] should apply to all overload resolution context for the function template.

jensmaurer commented 10 months ago

I'm not seeing a problem.

You quoted [over.over] p3:

The specialization, if any, generated by template argument deduction ([temp.over],

Since template argument deduction failed, no specialization was generated, and thus nothing is added to the candidate set. The "if any" explicitly acknowledges the possibility of failure.

If some compilers reject, please post bug reports to them.

xmh0511 commented 10 months ago

The "if any" explicitly acknowledges the possibility of failure.

If "if any" can clearly convey the meaning, the phrasing does not need to reference to [temp.over] because the deduction can also fail in the function template call context.

jensmaurer commented 10 months ago

So, do you claim the cross-reference to [temp.over] shouldn't be there, because it refers to a function call situation?

xmh0511 commented 10 months ago

So, do you claim the cross-reference to [temp.over] shouldn't be there, because it refers to a function call situation?

I meant "if any" may not be clear to apply to the case when template argument deduction fails. It may mean there is no template function. The argument is, that we have [temp.over] to specify that the function won't be added to the candidate set if the deduction fails, hence, [over.over] should also have a similar formal wording like that.