yuri kilochek
yuri kilochek

Reputation: 13484

Addresses of template function instantiations over T and T const

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?

Upvotes: 2

Views: 86

Answers (2)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507185

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.

Upvotes: 7

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153975

I would expect that the functions are non-equal. The standard definitely does not require them to be identical although I suspect that it allows them to be identical (I wouldn't quite know where to look for the corresponding clause).

Upvotes: 0

Related Questions