【发布时间】:2018-02-12 18:23:31
【问题描述】:
为什么不能用同一个指针的偏移量实例化带有指针模板参数包的模板函数?
我的意思是:鉴于这段简短的代码,为什么我必须注释掉最后两行?
template <int * ... pt> void f() {}
int n[] = {1, 2, 3};
int m = n[1];
int main()
{
f<n>(); // this is accepted
f<n, &m>(); // this is accepted too
//f<n, n+1>(); // this is not.
//f<n, &n[1]>(); // this isn't accepted neither
}
n+1 不代表与 &m 相同的地址吗?还是联动有区别?或者还有什么?
【问题讨论】:
-
m是它自己的变量;&m与n无关。 -
试试
int * p = &n[0]; f<p>();以获得很好的解释。
标签: c++ templates language-lawyer variadic-templates