首页 > 解决方案 > C++ DR 115: Address of template specialization even if deduction fails

问题描述

Defect Report 115 (http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#115) says that if a template-id identifies a single function template specialization, it is considered to be an lvalue for that function template specialization, if template argument deduction (for the template arguments of the function template named by the template-name inside the template-id, I suppose) is not done, or it is done, but it fails.

I believe that the purpose of this defect report is to allow programs such as:

template <typename Fn> 
void f(Fn param); 

template <typename T> 
void g(T); 

int main() 
{ 
    f(g<int>); 
} 

This is because in the call f(g<int>), the type of param (Fn) is a non-deduced context since its respective argument used to be (before this defect report) a function template. This is covered by the "in contexts where template argument deduction is not done" part of the defect report.

What I have trouble understanding is why does the defect report also mention situations where template argument deduction is done, but fails. Why do these contexts need to be explicitly mentioned in the defect report? These situations are referred to explicitly both in [temp.arg.explicit] p. 4 and in [over.over] p. 2.

I came up with an example which may be covered by the "contexts in which template argument deduction is done, but fails" part of the defect report, however I highly doubt that this was the reason why this part was added to the defect report. The example is shown below:

template <int i> 
struct B {}; 

template <short i = 0> 
void f(B<i>); 

int main() 
{ 
    void (*pf) (B<0> param); 
    pf = f<>(); 
} 

In the above example template argument deduction fails because the non-type template parameter i of function template f is of type short, while the non-type template parameter i of class template B is of type int, and this situation is explicitly forbidden by the standard (at the time of writing this is specified in [temp.deduct.type] p. 19). However, the default template argument for the non-type template parameter i of function template f is exactly the required value (0, as in the type of pf), so this program should now be correct. However, as I said before, I do not think this is the example the authors of this defect report had in mind.

Why do contexts where deduction fails have to be mentioned explicitly in the standard?

Thank you for your help!

标签: c++templateslanguage-lawyer

解决方案


推荐阅读