地址的c ++模板函数 [英] address of c++ template function

查看:117
本文介绍了地址的c ++模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么无法编译? (g ++ - 4.5)

Why does this fail to compile? (g++-4.5)

template < typename U >
static void h () {
}

int main () {
  auto p = &h<int>; // error: p has incomplete type
}

EDIT :这是一个解决方法:

EDIT: Here is a work-around:

template < typename U >
static void h () {
}

int main () {
  typedef decltype (&h<int>) D;
  D p = &h<int>; // works
}


推荐答案

+ 0x这保证工作。然而在C ++ 03这不工作(初始化部分,也就是)和一些编译器显然不支持它。

In C++0x this is guaranteed to work. However in C++03 this wasn't working (the initializer part, that is) and some compilers apparently don't support it yet.

此外,我记得C ++ 0x字词不清楚& h< int> 当它是一个函数模板的参数,并推导出相应的参数(这是 auto 被翻译为,概念)。然而,意图是,它是有效的。请参阅此缺陷报告,其中他们设计了措辞,示例以 Nico Josuttis和他们的最后一个例子。

Furthermore, I remember that the C++0x wording is not clear what happens with &h<int> when it is an argument to a function template and the corresponding parameter is deduced (this is what auto is translated to, conceptionally). The intention is, however, that it is valid. See this defect report where they designed the wording, the example by "Nico Josuttis" and their final example.

有另一个规则,措词强制,但编译器没有正确实现。例如,查看此ang PR

There is another rule that the wording enforces but compilers are not correctly implementing. For example, see this clang PR.

这篇关于地址的c ++模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆