MSVC++2010 中的部分模板专业化(默认)
以下代码片段无法在 MSVC++2010 上编译(使用 gcc、icc 和 sun CC 可以正常编译):
#include <iostream>
template< class T, unsigned D > struct Attribute
{
T attr[D];
};
template< class T, unsigned D, class A = Attribute< T, D > > struct Point
{
T coor[D];
A a;
};
template< class P1, class P2 > struct Pair;
template< class T1, class T2, unsigned D > struct Pair< Point< T1, D>, Point< T2, D > >
{
Point< T1, D> p1;
Point< T2, D> p2;
static const char * id()
{
return "specialized";
}
};
int main()
{
Pair< Point< float, 3>, Point< double, 3> > p;
std::cout << p.id() << std::endl;
return 0;
}
如果我从 Point
声明中删除 class A
的默认值,则编译得很好。对于如何在不更改 Pair 的非专用声明(即 template
)的情况下解决此问题的任何建议,我们将不胜感激。删除实际代码中的默认值也不是一个选择。
error C2079: 'p' uses undefined struct 'Pair<P1,P2>'
with
[
P1=Point<float,3>,
P2=Point<double,3>
]
The following snippet won't compile on MSVC++2010 (compiles fine with gcc, icc and sun CC):
#include <iostream>
template< class T, unsigned D > struct Attribute
{
T attr[D];
};
template< class T, unsigned D, class A = Attribute< T, D > > struct Point
{
T coor[D];
A a;
};
template< class P1, class P2 > struct Pair;
template< class T1, class T2, unsigned D > struct Pair< Point< T1, D>, Point< T2, D > >
{
Point< T1, D> p1;
Point< T2, D> p2;
static const char * id()
{
return "specialized";
}
};
int main()
{
Pair< Point< float, 3>, Point< double, 3> > p;
std::cout << p.id() << std::endl;
return 0;
}
If I remove a default for class A
from Point
declaration it compiles just fine. Any suggestions on how to work around this issue without changing non-specialized declaration of Pair (i.e., template< class P1, class P2 > struct Pair;
) are greatly appreciated. Removing defaults in the real code is not an option either.
error C2079: 'p' uses undefined struct 'Pair<P1,P2>'
with
[
P1=Point<float,3>,
P2=Point<double,3>
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Point 的第三个模板参数应该出现在专业化中:
Point's 3rd template parameter should appear in the specialization: