使函数模板专业化虚拟合法吗?
在 C++ 中,函数模板特化的行为应该与普通函数完全相同。 这是否意味着我可以制作一个虚拟的?
例如:
struct A
{
template <class T> void f();
template <> virtual void f<int>() {}
};
struct B : A
{
template <class T> void f();
template <> virtual void f<int>() {}
};
int main(int argc, char* argv[])
{
B b;
A& a = b;
a.f<int>();
}
Visual Studio 2005 给出以下错误:
致命错误 C1001:编译器中发生内部错误。
In C++, a function template specialization is supposed to act exactly like a normal function. Does that mean that I can make one virtual?
For example:
struct A
{
template <class T> void f();
template <> virtual void f<int>() {}
};
struct B : A
{
template <class T> void f();
template <> virtual void f<int>() {}
};
int main(int argc, char* argv[])
{
B b;
A& a = b;
a.f<int>();
}
Visual Studio 2005 gives me the following error:
fatal error C1001: An internal error has occurred in the compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不错的编译器错误。 对于这种类型的检查,我总是先使用 Comeau 编译器,然后再返回标准并进行检查。
现在,正如另一位用户所发布的那样,事实是该标准不允许您定义虚拟模板化方法。 基本原理是,对于所有虚拟方法,必须在 vtable 中保留一个条目。 问题是模板方法只有在实例化(使用)时才会被定义。 这意味着 vtable 最终在每个编译单元中将具有不同数量的元素,具体取决于发生了多少次对不同类型的 f() 的不同调用。 那么地狱就会出现...
如果您想要的是其参数之一的模板化函数,并且一个特定版本是虚拟的(请注意参数的部分),您可以这样做:
如果您希望将此泛化为任何类型,那么你运气不好。 考虑另一种类型的委托而不是多态(聚合+委托可能是一个解决方案)。 有关当前问题的更多信息将有助于确定解决方案。
Nice compiler error. For this type of checks I always fallback to the Comeau compiler before going back to the standard and checking.
Now, as it has been posted by another user, the fact is that the standard does not allow you to define virtual templated methods. The rationale is that for all virtual methods, an entry must be reserved in the vtable. The problem is that template methods will only be defined when they have been instantiated (used). This means that the vtable would end up having a different number of elements in each compilation unit, depending on how many different calls to f() with different types happen. Then hell would be raised...
If what you want is a templated function on one of its arguments and one specific version being virtual (note the part of the argument) you can do it:
If you want this generalized for any type, then you are out of luck. Consider another type of delegation instead of polymorphism (aggregation + delegation could be a solution). More information on the problem at hand would help in determining a solution.
根据 http://www.kuzbass.ru:8086/docs/isocpp/ template.html ISO/IEC 14882:1998:
例子:
According to http://www.kuzbass.ru:8086/docs/isocpp/template.html ISO/IEC 14882:1998:
Example:
正如其他人所指出的,这不是合法代码,因为成员函数模板不能声明为虚拟的。
然而,即使是 Visual Studio 2012 也对此感到窒息:
单击此处查看完整尺寸
事件日志表明编译器崩溃于
0xC0000005
,或STATUS_ACCESS_VIOLATION
。 有趣的是,某种(非法)代码构造如何使编译器出现段错误......As others have noted, this is not legal code because a member function template cannot be declared
virtual
.Yet even Visual Studio 2012 chokes on this:
Click here for full size
Event logs indicate that the compiler crashed on
0xC0000005
, orSTATUS_ACCESS_VIOLATION
. It's funny how a certain (illegal) code construct can make the compiler segfault...