template type deduction handled improperly with implicit overload resolution
| Bugzilla Link | 43287 |
| Version | trunk |
| OS | All |
| CC | @dwblaikie,@DougGregor,@zygoloid |
Extended Description
I believe that clang is handling template type deduction improperly for forward declarations of templates with overload resolution. Consider the following:
#include <type_traits>
template<typename U>
int f(const U t);
int v;
auto test = f(v);
template<typename U>
int f(U t) {
static_assert(!std::is_const<decltype(t)>::value, "t should be non-const");
return 0;
}
GCC correctly identifies this as int rather than const int. I believe that the type information is being preserved from the canonical decl from which this overload is being referenced. This was reduced from another test case which results in an invalid function call.
Sorry, I couldn't really come up with a good summary
@llvm/issue-subscribers-clang-frontend
Author: Saleem Abdulrasool (compnerd)
Extended Description
I believe that clang is handling template type deduction improperly for forward declarations of templates with overload resolution. Consider the following:
#include <type_traits>
template<typename U>
int f(const U t);
int v;
auto test = f(v);
template<typename U>
int f(U t) {
static_assert(!std::is_const<decltype(t)>::value, "t should be non-const");
return 0;
}
GCC correctly identifies this as int rather than const int. I believe that the type information is being preserved from the canonical decl from which this overload is being referenced. This was reduced from another test case which results in an invalid function call.
Sorry, I couldn't really come up with a good summary
Hi, I think this is false issue and can be closed. Function arguments after implicit conversion to match function parameter is treated as converted value, and not as the value it was passed.
Here Implicit conversion transforms the non-const value to const value and that's why the assert fails.
https://godbolt.org/z/T6dGGjP3j
As @shafik pointed out to me offline, there is split between implementations: https://godbolt.org/z/xcebMo164
Yes, but if you rearrange the code like this https://godbolt.org/z/j71j58vMG it's treated as int and the code works fine for every implementation. So the problem is only when the template function is forward declared.
https://godbolt.org/z/aever9j3h and if the it's normal function instead of a templated function then also it's treated int. So I can work on unifying the behavior where templated functions behave the same way a normal function does? Any Inputs?
I still want us to understand Standard's point of view on this example before making any moves. The fact that Clang is not the only compiler that behaves the current way (EDG matches us), makes me even more cautious.
Clang is definitely wrong here. The parameter types of the function are adjusted during template argument deduction (see [temp.deduct.general] p3), but the types of the parameter declarations does not change (there is also a note in the linked wording which says as much).
This happens because template argument yields the canonical declaration of a specialization, and we add the parameters from that specialization into scope when instantiating the function definition (this is done by Sema::addInstantiatedParametersToScope).
See https://cplusplus.github.io/CWG/issues/1001.html for some related fun
The behavior of clang is correct here.
https://eel.is/c++draft/temp.deduct.general#3 Here in the example we have similar case.
template <class X> void g(const X x); the function template is defined and
// # 3: function type is g(int), x is const
g<int>(1);
// # 4: function type is g(int), x is const
g<const int>(1); in the both the case is const
@mahtohappy
- Example in the Standard is not concerned with type deduction: it passes template arguments explicitly. Whereas in this issue type deduction is happening.
- Example in the Standard has single declaration (with definition) for each function template. In this example we have two declarations, and I still consider it not proven than they correspond (i.e. they are not two distinct primary templates).
https://eel.is/c++draft/dcl.fct#5 in this example
void f(char[]) {} // defines #1
void f(const char*) {} // OK, another overload
the const makes it another overload function.
Similar way,
template<typename U>
int f(const U t); //first
and
template<typename U>
int f(U t); //second
should be two different overload and should be chosen based on the argument we're passing. If the argument is int it compiler should choose second function and if argument is const int compiler should choose first to call. Is my correct here?
https://godbolt.org/z/ddeM7f9b8 here clang complains about redefinition of f which it shouldn't, right?
https://godbolt.org/z/djWE17vxc in our example, we only have declaration of first and and definition of second, as we're passing int in the function call it should use the //second function to call, where the v isn't const and the static assert should pass.
Function templates are not functions, so it's not obvious that [dcl.fct]/5 applies to them. The fact that CWG1322 "Function parameter type decay in templates" is still not resolved underlines that.
See also the proposed resolution for CWG1668 and relevant discussion on the core reflector.
Assuming template<typename T> void f(T t); and template<typename T> void f(const T t); declare the same function template, the consensus seems to be that the type of t is int in the first declaration and const int in the second when T is int.
https://godbolt.org/z/TMMGbc73s but if you try to define these function they're complaining of redefinition. Is this also wrong?