开发者

does it makes sense a virtual template method?

Suppose a construct like this:

class Interface
{
public:
   template <typename T>
   virtual void reportOperationError(T code , std::string message) = 0;
};

i don't understand the use case for this thing, in which case it is useful, and how?

In开发者_运维百科 case you wonder, I haven't seen this code anywhere, just want to understand if this could have some particular use


Templated member functions cannot be virtual... Each instantiation of the function will add another entry to the virtual table, and the compiler will have to go over all of the code in order to create the vtable. Therefore, regardless of it being useful or not, it's just not legal C++.


This isn't legal in C++. Member function templates cannot be virtual because the size of the VTable would be dependent on instantiations in other translation units which would make it very very hard for compiler implementers.

This would ruin the whole point of C++ compilation model, that is, separate compilation. A C++ translation Unit (AKA source file) must be self-sufficient to be compiled. If the member function tamplates are allowed, this gets nearly impossible


It's not possible to make virtual member function a template. This code will not compile, regardless of its potential utility.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜