Created on 2009-11-08.00:00:00 last changed 8 months ago
Additional notes (February, 2024)
There is implementation divergence for the following example (clang and EDG accept, gcc and MSVC reject):
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; }
Possible resolution:
Change in 9.3.4.6 [dcl.fct] paragraph 6 as follows:
The type of a function is determined using the following rules. The type of each parameter (including function parameter packs) is determined from its own parameter-declaration (9.3 [dcl.decl]). After determining the type of each parameter, any parameter of type “array of T” or of function type T is adjusted to be “pointer to T”. After producing the list of parameter types, any top-level cv-qualifiers modifying a non-dependent parameter type are deleted when forming the function type. The resulting list of transformed parameter types and the presence or absence of the ellipsis or a function parameter pack is the function's parameter-type-list. ...
Rationale (November, 2010):
The CWG agreed that this behavior is intrinsic to the different ways cv-qualification applies to array types and non-array types.
Notes, January, 2012:
Additional discussion of this issue arose regarding the following example:
template<class T> struct A { typedef double Point[2]; virtual double calculate(const Point point) const = 0; }; template<class T> struct B : public A<T> { virtual double calculate(const typename A<T>::Point point) const { return point[0]; } }; int main() { B<int> b; return 0; }
The question is whether the member function in B<int> has the same type as that in A<int>: is the parameter-type-list instantiated directly (i.e., using the adjusted types) or regenerated from the individual parameter types?
(See also issue 1322.)
According to 9.3.4.6 [dcl.fct] paragraph 5, top-level cv-qualifiers on parameter types are deleted when determining the function type. It is not clear how or whether this adjustment should be applied to parameters of function templates when the parameter has a dependent type, however. For example:
template<class T> struct A { typedef T arr[3]; }; template<class T> void f(const typename A<T>::arr) { } // #1 template void f<int>(const A<int>::arr); template <class T> struct B { void g(T); }; template <class T> void B<T>::g(const T) { } // #2
If the const in #1 is dropped, f<int> has a parameter type of A* rather than the const A* specified in the explicit instantiation. If the const in #2 is not dropped, we fail to match the definition of B::g to its declaration.
History | |||
---|---|---|---|
Date | User | Action | Args |
2024-02-28 22:18:58 | admin | set | status: drafting -> review |
2024-02-03 15:07:21 | admin | set | messages: + msg7587 |
2012-02-27 00:00:00 | admin | set | status: open -> drafting |
2012-01-17 00:00:00 | admin | set | status: nad -> open |
2010-11-29 00:00:00 | admin | set | messages: + msg3213 |
2010-11-29 00:00:00 | admin | set | status: open -> nad |
2009-11-08 00:00:00 | admin | create |