Tho*_*mas 20 c++ templates function-pointers c++11
为什么这不能编译?(克++ - 4.5)
template < typename U >
static void h () {
}
int main () {
auto p = &h<int>; // error: p has incomplete type
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是一个解决方法:
template < typename U >
static void h () {
}
int main () {
typedef decltype (&h<int>) D;
D p = &h<int>; // works
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*itb 13
在C++ 0x中,这保证可以工作.然而,在C++ 03中,这不起作用(初始化器部分,即),并且一些编译器显然还不支持它.
此外,我记得C++ 0x的措辞不清楚&h<int>当它是函数模板的参数并推导出相应的参数时会发生什么(这是从auto概念上翻译的内容).但是,它的意图是有效的.请参阅此缺陷报告,他们设计了措辞,"Nico Josuttis"的示例及其最终示例.
还有另一条规则是措辞强制执行,但编译器没有正确实施.例如,看到这个clang PR.