Is it ever possible that in the following setup
template <typename T>
inline void id() {
//...
}
template <typename T>
bool check() {
return &id<T> == &id<T const>;
}
check
will return true
for some T
? Does it depend on what is being done inside id
? What does the standard have to say about this?
Sure. Try const int
or int&
or void()
.
There's a rule that top-level const
qualifiers collapse if you get multiple of them through a typedef
or a template argument, which means check<int const>()
will return true
.
[normative text pending]
Then there's a rule that ignores top-level const
on things it doesn't work on, like references or function types. That means check<int&>
and check<int()>
will return true
.
§8.3.2 [dcl.ref] p1
Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef (7.1.3) or of a template type argument (14.3), in which case the cv-qualifiers are ignored.
and
§4.4 [conv.qual] p3
[ Note: Function types (including those used in pointer to member function types) are never cv-qualified (8.3.5). —end note ]
§8.5.3 [dcl.fct] p6
The effect of a cv-qualifier-seq in a function declarator is not the same as adding cv-qualification on top of the function type. In the latter case, the cv-qualifiers are ignored.